碎片知识:arguments.callee&部分JS笔试题

128 阅读1分钟

1.arguments.callee:输出函数自身引用

例:立即执行函数输出1-100的阶乘

var num = (function(n){
   if(n == 1){
     return 1;
}
return n * arguments. callee(n-1);
}(100))

2.this指向

1)

var foo = 123;
function print(){
  this.foo = 234;//全局变量都归window ,go的foo变成234
  console.log(foo);
}
print();//234

2)

var foo = 123;
function print(){
//var this = Object.create(print.prototype)
  this.foo = 234;
  console.log(foo);//AO里无foo去全局找
}
 new print();//123

3.构造函数 

运行test()和new test()的结果
var a = 5;
function test(){ 
//new test()的时候:var this =  Object.create(test.prototype) 此处this无a
   a = 0;         
   alert(a);      //0                                        //0
   alert(this.a);    //5                                     //undefined
   var a;
   alert(a);      //0                                        //0
}
AO{                                   
   a : 0;
   this:window;
}
test();     
---------------

AO{                                   
   a : 0;
   this:{};
}
new test();                           

4.原型链

var bar = {a:"002"};
function(){
   bar.a = 'a';
   Object.prototype.b = 'b';
   return function inner(){
     console.log(bar.a);//a
     console.log(bar.b);//b
   }
}
print()();