的
this关键字是指功能所属的对象,或
window对象如果函数不属于任何对象。
在OOP代码中使用它来引用该函数所属的类/对象,例如:
function foo() { this.value = 'Hello, world'; this.bar = function() { alert(this.value); }}var inst = new foo();inst.bar();
这提醒:
Hello, world
您可以
this使用
apply()或
call()函数 *** 纵引用的对象。 (有时非常方便)
var bar1 = new function() { this.value = '#1';}var bar2 = new function() { this.value = '#2';}function foo() { alert(this.value);}foo.call(bar1); // Output: #1foo.apply(bar2, []); // Output: #2
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)