JS call bind apply

来源:互联网 发布:自建邮件服务器 linux 编辑:程序博客网 时间:2024/06/01 01:33
  1. function a(xx, yy) {     
  2.     alert(xx, yy);     
  3.     alert(this);     
  4.     alert(arguments); 
  5. a.apply(null, [555]); 
  6. a.call(null555); 
  1. function a(xx) {         
  2.     this.b = xx; 
  3. var o = {}; 
  4. a.apply(o, [5]); 
  5. alert(a.b);    // undefined 
  6. alert(o.b);    // 5 
//测试
var m = {    "x" : 5,};alert( m.x + 6 );   //11function foo(y) {    alert( this.x + y );}foo( 5 );           //Nanfoo.call(m , 6);    // 11foo.apply( m, [7]); // 12var foo1 = foo.bind( m, 8);foo1();             // 13
 在foo中并不存在this.x ,
 而call apply bing 相当于给继承m的x属性
继承的时候改变this的指向,foo里面的对象m变成this。
1 0
原创粉丝点击