Navigator 2.0, Internet Explorer 3.0; enhanced in Navigator 3.0
function functionname(argname1 [, ... argname_n]) { body }
new Function([argname1 [, ..., argname_n]], body) Navigator 3.0 only
Any number of string arguments, each one of which names one argument of the Function object being created.
A string that specifies the body of the function. It may contain any number of JavaScript statements, separated with semicolons.
The newly created Function object.
An array of arguments that were passed to the function.
The number of elements in the arguments[] array.
A reference to the Function object that invoked this this one, or null if the function is invoked at the top level.
An object which, for constructor functions, defines properties and methods that will be shared by all objects created with that constructor function.
Return a string representation of the function.
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.
file: /Techref/language/java/script/definitive/refp_143.htm, 10KB, , updated: 2019/10/14 15:00, local time: 2024/12/28 07:26,
52.14.49.59: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? <A HREF="http://linistepper.com/techref/language/java/script/definitive/refp_143.htm"> [Chapter 21] Reference: Function</A> |
Did you find what you needed? |