建设银行网站首页打,网页设计的风格,英文网站提交,做拍福利爱福利视频网站1.在一般函数方法中使用 this 指代全局对象 function test(){this.x 1;alert(this.x);}test(); // 12.作为对象方法调用#xff0c;this 指代上级对象 function test(){alert(this.x);
}
var o {};
o.x 1;
o.m test;
o.m(); // 1 3.作为构造函数调用#xff0c;this 指代…1.在一般函数方法中使用 this 指代全局对象 function test(){this.x 1;alert(this.x);}test(); // 12.作为对象方法调用this 指代上级对象 function test(){alert(this.x);
}
var o {};
o.x 1;
o.m test;
o.m(); // 1 3.作为构造函数调用this 指代new 出的对象 function test(){this.x 1;}var o new test();alert(o.x); // 1//运行结果为1。为了表明这时this不是全局对象我对代码做一些改变var x 2;function test(){this.x 1;}var o new test();alert(x); //2 4.apply 调用 apply方法作用是改变函数的调用对象此方法的第一个参数为改变后调用这个函数的对象this指代第一个参数 var x 0;function test(){alert(this.x);}var o{};o.x 1;o.m test;o.m.apply(); //0
//apply()的参数为空时默认调用全局对象。因此这时的运行结果为0证明this指的是全局对象。如果把最后一行代码修改为
o.m.apply(o); //1 转载于:https://www.cnblogs.com/pabitel/p/5922511.html