please dont rip this site

JavaScript: The Definitive Guide

Previous Chapter 21
JavaScript Reference
Next
 

Function Object

Name

Function Object---a JavaScript function

Availability

Navigator 2.0, Internet Explorer 3.0; enhanced in Navigator 3.0

Synopsis

function functionname(argname1 [, ... argname_n])
{
     body
}

Constructor

new Function([argname1 [, ..., argname_n]], body) Navigator 3.0 only

Arguments

argname1, ..., argname_n

Any number of string arguments, each one of which names one argument of the Function object being created.

body

A string that specifies the body of the function. It may contain any number of JavaScript statements, separated with semicolons.

Returns

The newly created Function object.

Properties

arguments[]

An array of arguments that were passed to the function.

arguments.length

The number of elements in the arguments[] array.

caller

A reference to the Function object that invoked this this one, or null if the function is invoked at the top level.

prototype

An object which, for constructor functions, defines properties and methods that will be shared by all objects created with that constructor function.

Methods

toString()

Return a string representation of the function.

Description

A function is a fundamental data type in JavaScript. Chapter 6, Functions, explains how to define and use functions. The Function object is an object wrapper around the basic function data type; this object type exists so that functions can have properties and methods associated with them. When a function value is used in an "object context"--i.e., when you attempt to invoke a method or read a property of a function, JavaScript internally converts the function value into a temporary Function object, so that the method can be invoked or the property value read.

In Navigator 3.0, you can also use the Function() constructor method shown above to create your own Function objects. Functions defined in this way are sometimes called "anonymous" functions, because they are not given a name when they are created. Chapter 6, Functions discusses the definition and use of anonymous functions. Just as JavaScript converts from a function value to a Function object whenever necessary, so it will convert from a Function object (created with the Function() constructor) to a function value whenever you use the object in a function value context--i.e., whenever you invoke it with the () operator. This conversion from Function object to function value is done by the valueOf() method.

The arguments[] and caller properties of the Function object are unusual because they may only be accessed from within the body of a function. That is, they are only defined for a Function object while that function is being executed. There is no special keyword in JavaScript that refers to the currently executing function, so you must refer to Function objects by name. For example:

function myfunc() 
{ 
    if (myfunc.arguments.length ==index.html 0) return; 
        ...
}

Because JavaScript is loosely typed language, it does not care what number or type of arguments you pass to a function, even if you provided argument names when you defined the function. The arguments[] property is an array containing whatever arguments were passed to the function. You can use this array to implement functions that can handle any number of arguments correctly.

The caller property refers to the function that called this one. You can print out the caller for debugging purposes, and you can even use this property to invoke the calling function--creating a strange sort of recursion.

Note that the arguments property is actually just a reference to the Function object itself. Therefore, instead of typing function.arguments[i] and function.arguments.length you can just use function[i] and function.length. Although using the arguments property is unnecessary, it often makes for clearer code, and is the officially supported way to access function arguments.

Finally, the prototype property of the Function object is a very powerful one. It refers to a "prototype" object from which other objects "inherit" properties and methods. When a function is used as a constructor function, any objects created with that constructor will inherit the properties and methods defined in the prototype object of the constructor. Prototype objects and the prototype property are discussed in Chapter 7, Objects.

See Also

"Object", Chapter 6, Functions, Chapter 7, Objects


Previous Home Next
frames[] Book Index Function.arguments[]

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell

file: /Techref/language/java/script/definitive/refp_143.htm, 10KB, , updated: 2019/10/14 15:00, local time: 2024/11/23 15:28,
TOP NEW HELP FIND: 
3.144.249.63:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://techref.massmind.org/Techref/language/java/script/definitive/refp_143.htm"> [Chapter 21] Reference: Function</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to massmind.org!

 

Welcome to techref.massmind.org!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .