[JavaScript] Function Properties and Methods

来源:互联网 发布:php 函数等于双引号 编辑:程序博客网 时间:2024/06/06 00:08

arguments其实就是此函数体内的一个对象,arguments.length 指的是此函数被调用时,实际的参数

arguments.callee.length 此函数的期待的参数个数(当然,这个没啥意义,因为,JavaScript中,调用函数时,参数放多少个都无所谓)

Function的Methods其实就是指的 call() 和 apply() 这两个函数


The typeofoperator returns the string“function” when applied to a function.
But functions are really a specialized kind of JavaScript object, just like the Date and RegExp objects, they also have properties and methods

Within the body of a function, the length property of the arguments array specifies the number of arguments that were passed to the function

The length property of a function itself, a read-only property means the number of arguments that this function expects to be passed, the number of parameters it declares in its parameter list.


Regardless of the number of parameters a function declares, a function can be invoked with any number of arguments, and these arguments can be retrieved through the arguments array.

The length property is available both inside and outside of the function body, but arguments.length can only be invoked inside the function body.

The prototype Property
A prototype property refers to a predefined prototype object, which comes into play when the function is used as a constructor with the new operator

Defining You Own Function Properties
Use a property of the Function object, and this way we won’t clutter up the namespace by defining a global variable

Requirment: a function should return a unique integer whenever it’s invoked
This function should keep track of the values it has already returned, and this info must persist across function invocations. Since the info is usedonly by the function itself. It’s better to store the info in a property of this Function Object.
See the demo: defineFunctionOwnProperty()


The apply() and call() Methods
任何function对象(即任何一个function)都默认含有两个method, call() 和 apply().
假设我们有方法f()
f.call(obj, 1, 2)的含义就是:
1、在对象obj上,调用f()方法
2、调用时,传递两个参数1, 2
3、function f的函数体内,this关键字的值就是obj


f.call(obj, 1, 2)也等价于:
obj.marvinMethod = f;
obj.marvinMethod(1, 2);
delete obj.marvinMethod;

f.apply(obj, [1, 2])也是一样的,只不过,传递是第二个参数是个数组


<html><head><script type="text/javascript">function check(myPara) {    var actual = myPara.length;//6, demoFuncArguLength()'s actual    var expected = myPara.callee.length;//3, demoFuncArguLength()'s expected        var selfActual = arguments.length;//2, check()'s actual    var selfExpected = arguments.callee.length;//1, check()'s expected        var allKindsLength = "myPara.length="+myPara.length+    ",\nmyPara.callee.length="+myPara.callee.length+    ",\narguments.length="+arguments.length+    ",\narguments.callee.length="+arguments.callee.length;    alert(allKindsLength);}function demoFuncArguLength(x, y, z) {    check(arguments, 'hellomarvin');    // Now do the rest of the function normally    return x + y + z;}// Create and initialize the "static" variable.// Function declarations are processed before code is executed, so// we really can do this assignment before the function declaration.defineFunctionOwnProperty.counter=0;function defineFunctionOwnProperty() {    // Increment and return our "static" variable    defineFunctionOwnProperty.counter++;    alert(defineFunctionOwnProperty.counter);    return defineFunctionOwnProperty.counter;}</script></head><body><form><input type="button" name="demojavascript" value="Demo Function and argument length" onclick="demoFuncArguLength(1, 2, 3, 'a', 'b', 'c');"/><input type="button" name="demojavascript2" value="Demo Function Own Property" onclick="defineFunctionOwnProperty();"/></form></body></html>


原创粉丝点击