[JavaScript] Function Properties and Methods

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

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

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


The typeof operator 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 used only 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" 
							οnclick="demoFuncArguLength(1, 2, 3, 'a', 'b', 'c');"/>
			<input type="button" name="demojavascript2" value="Demo Function Own Property" 
							οnclick="defineFunctionOwnProperty();"/>
		</form>
	</body>
</html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值