摘要: bind(a,b)// bind方法中:a代表把this指向a的作用域 b代表参数 new 绑定代码 function foo(p1,p2){ this.val=p1+p2;}var bar=foo.bind(null,"p1","p2"); //把函数foo的引用附给了bar 并把foo的this 阅读全文
posted @ 2017-09-19 15:23 _increase 阅读(430) 评论(0) 推荐(0) 编辑
摘要: 简单的栗子: function(){ console.log(this.a);}var obj={ a:2 }; foo.call(obj);//2 call把foo中的this指向了obj 注意↓ "装箱" 当第一个参数传入的是:字符串类型、布尔、数字类型 这个原始值会被转换成它的对象形式 new 阅读全文
posted @ 2017-09-15 18:17 _increase 阅读(103) 评论(0) 推荐(0) 编辑
摘要: 先上代码: function foo(num){ console.log("foo:"+num); this.count++;} foo.count=0; var i; for(i=0;i<10;i++){ if(i<5){ foo(i); } } 输出 6.7.8.9 console.log(fo 阅读全文
posted @ 2017-09-15 16:55 _increase 阅读(99) 评论(0) 推荐(0) 编辑
摘要: this指向函数作用域,对也错 function foo(){ var a=2; this.bar();//this 指向window}function bar(){ console.log(this.a);//指向window } foo(); //RegereceError: a is not 阅读全文
posted @ 2017-09-15 16:54 _increase 阅读(167) 评论(0) 推荐(0) 编辑