1、foo()结果
1 function foo() { 2 bar.apply(null, arguments); 3 } 4 function bar(){ 5 console.log(arguments); 6 } 7 8 foo(1,2,3,4,5);
结果:(1,2,3,4,5)
2、alert的结果
1 function b(x, y, a){ 2 arguments[2] = 10; 3 alert(a); 4 } 5 b(1, 2, 3);
结果:10
3、typeof f
1 var f = ( 2 function f(){ 3 return "1"; 4 }, 5 function g(){ 6 return 2; 7 } 8 )(); 9 typeof f;
结果;"number"
逗号运算符:(x,y),输入结果是:两个值进行比较,谁大输出谁
4、x
1 var x = 1; 2 if (function f(){}) { 3 x += typeof f; 4 } 5 console.log(x);
结果:"1undefined"
5、求100的阶乘
var num = (function(n){ if (n == 1){ return 1; } return n * arguments.callee(n-1); }(100))
6、this
1 var foo = '123'; 2 function print(){ 3 var foo = '456'; 4 this.foo = '789'; 5 console.log(foo); 6 } 7 print();
结果:456
1 var foo = '123'; 2 function print(){ 3 this.foo = '789'; 4 console.log(foo); 5 } 6 print();
结果:789
1 var foo = '123'; 2 function print(){ 3 this.foo = '789'; 4 console.log(foo); 5 } 6 new print();
结果:123
7、print()()
1 var bar = {a: "002"}; 2 function print(){ 3 bar.a = 'a'; 4 Object.prototype.b = 'b'; 5 return function inner() { 6 console.log(bar.a); 7 console.log(bar.b); 8 } 9 } 10 print()();
结果:a
b
8、深拷贝
1 var bar = {a: "002"}; 2 function print(){ 3 bar.a = 'a'; 4 Object.prototype.b = 'b'; 5 return function inner() { 6 console.log(bar.a); 7 console.log(bar.b); 8 } 9 } 10 print()();
9、类数组
1 var obj = { 2 "2" : "a", 3 "3" : "b", 4 "length" : 2, 5 "push" : Array.prototype.push 6 } 7 obj.push("c"); 8 obj.push("d"); 9 10 //obj为?
结果:{2: "c", 3: "d", length: 4} 解析:类属性 属性要为索引,必须要有length,最好有push Array.prototype.push = function(target) { obj[obj.length] = target; obj.length++; }