`

8 Function

阅读更多

1. arguments

 

在函数内部,arguments表示传入进来的参数,是个类似数组的Object。

 

		function add(x, y, z) {
			if (arguments.length != 3) {
				throw new Error("f should called with 3 arguments!");
			}                                                        
			return x + y + z;
		}           
		
		function max(/* ... */) {
			var rst = Number.NEGATIVE_INFINITY;
			for (var i = 0 ; i < arguments.length; ++i) {
				if (arguments[i] > rst) {
					rst = arguments[i];
				}
			}                          
			return rst;
		}         
		     
	    var f = function(x) {if (x <= 1) return 1; else return x * arguments.callee(x-1);}
		document.write("1 + 2 + 3 = " + add(1, 2, 3) + "<br>");
		//! document.write("1 + 2 = " + add(1, 2) + "<br>"); //throws Error 
		document.write("the max of 1, -10, -100, 2000, 5000, -10000 is "
		               + max(1, -10, -100, 2000, 5000, -10000) + "<br>"
		               );                                                                    
		document.write("4! = " + f(4) + "<br>");           

 

 

2. Javascript对待function其实和数据是一样的。

 

function add(x,y) {return x + y;}
		function sub(x,y) {return x - y;}
		function mul(x,y) {return x * y;}
		function div(x,y) {return x / y;}
		
		function operate(operator, op1, op2) {
			return operator(op1, op2);
		}                             
		                                                              
		// i = (2 + 3) + (4 x 5)
		var i = operate(add, operate(add, 2, 3), operate(mul, 4, 5));   
		document.write("(2 + 3) + (4 x 5) = " + i + "<br>");     
 

 

3. 可以把一个function当作data赋值给一个object,这时这个function就是这个object的方法。

并且可以使用关键字this

 

 var calculator = new Object();
		calculator.op1 = 1;
		calculator.op2 = 2;
		calculator.op = function() {
			this.result = this.op1 + this.op2;
		}                                     
		calculator.op();
		document.write("1 + 2 = " + calculator.result + "<br>"); 
 

 

4. length 和 callee两个属性

 

function check(args) {
			if (args.length != args.callee.length) {
				throw new Error("Wrong number of arguments!!!");
			}
		}    
		function add1(x, y, z) {
			return x + y + z;
		}                                                   
		function add2(x, y, z) {
			check(arguments);
			return x + y + z;
		}                    
		document.write(add1("I", " love", " you!") + "<br>");
		document.write(add1("I", " love") + "<br>");
		document.write(add2("I", " love", " you!") + "<br>");
		document.write(add2("I", " love") + "<br>");                
 

 

5. call()和apply两个方法

 

 function foo(x) {
    	document.write(x + "<br>");
}    
foo(10);                          
foo.call(null,10); 
 

 

6. closure

 

7. Function -- 作用域是不太一样的

 

var y = "global";
function constructFunction() {
    var y = "local";
    return new Function("return y");  // Does not capture the local scope!
}
// This line displays "global" because the function returned by the
// Function() constructor does not use the local scope. Had a function
// literal been used instead, this line would have displayed "local".
alert(constructFunction()());  // Displays "global"
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics