【发布时间】:2012-06-27 01:55:59
【问题描述】:
可能重复:
JavaScript losing “this” object reference with private/public properties
为什么第二个警报显示的是窗口对象,而不是 O(甚至是 P)对象?
window.name = "test window";
O = {
name : 'object O',
f : function() {
alert(this.name); // 2nd alert
}
}
P = {
name : 'object P',
f : function() {
alert(this); // 1st alert
var of = O.f;
of();
}
}
P.f();
换句话说,对对象函数的直接调用怎么能在窗口的上下文中呢?我想这是一个关闭的问题,但我不知道切换发生在哪里。
谢谢。
【问题讨论】:
-
因为您已将
O.f与O分离。
标签: javascript window this