1.arguments.callee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
//经典的阶乘(递归)函数 <span style= "color: #0000ff;" > function </span> factorial(num) { if (num <= 1) { <span style= "color: #0000ff;" > return </span> 1; } else { <span style= "color: #0000ff;" > return </span> num * factorial(num - 1); } } //消除函数名的耦合现象 <span style= "color: #0000ff;" > function </span> factorial(num) { if (num <= 1) { <span style= "color: #0000ff;" > return </span> 1; } else { <span style= "color: #0000ff;" > return </span> num * arguments.callee(num - 1); //函数内部属性 [callee] } } var trueFactorial = factorial; factorial = <span style= "color: #0000ff;" > function </span> () { <span style= "color: #0000ff;" > return </span> 0; }; alert(trueFactorial(5)); //120 使用callee属性 接触函数名耦合状态 可以继续使用递归 否则返回0 alert(factorial(5)); //0 |
2.Length:表示函数希望接收的命名参数的个数
3.prototype:在创建自定义引用类型以及实现继承时,该属性的作用极为重要,该属性不可枚举,因此使用for-in 无法发现
方法:
1.apply()和call()
用途:在特定的作用域中调用函数,等于设置函数体内this对象的值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<span style= "color: #0000ff;" > function </span> <span style= "color: #ff0000;" >sum</span>(num1, num2) { <span style= "color: #0000ff;" > return </span> num1 + num2; } <span style= "color: #0000ff;" > function </span> callSum(num1, num2) { <span style= "color: #0000ff;" > return </span> <span style= "color: #ff0000;" >sum</span>.<span style= "color: #003300;" >call</span>( this , num1, num2); //明确传入每一个参数 } <span style= "color: #0000ff;" > function </span> callSum1(num1, num2) { <span style= "color: #0000ff;" > return </span> <span style= "color: #ff0000;" >sum</span>.<span style= "color: #333399;" >apply</span>( this , arguments); //传入arguments对象 } <span style= "color: #0000ff;" > function </span> callSum2(num1, num2) { <span style= "color: #0000ff;" > return </span> <span style= "color: #ff0000;" >sum</span>.apply( this , [num1, num2]); //传入数组 } alert(callSum1(10, 10)); //20 alert(callSum2(10, 10)); //20 |
2.bind():创建一个函数的实例,其this的值会被绑定到传给bind()函数的值
1
2
3
4
5
6
7
8
|
window.color = "red" ; var o = { color: "blue" }; <span style= "color: #333399;" > function </span> sayColor() { alert( this .color); } var objectSayColor=sayColor.bind(o); objectSayColor(); //blue; |
-END-