为什么窗口范围内的调用函数与对象范围不同
答
window.name
是吸气/设置器设置窗口的名称。因此,它必须是string
。
试试这个:
你会看到,现在window.name
设置为"something,else"
;这是Array.toString()
的结果。
这正是这里发生的情况。当您拨打object
的toString
时,会得到[object Object]
。
如果您不使用预定义的window.name
getter/setter,则您的程序可以正常工作。
function Person(first, last) {
this.something = {
first,
last
};
}
f = {};
Person.call(f, "fsd", "fsd");
console.log(f.something);
g = window;
Person.call(g, "fsd", "fsd");
console.log(g.something);
更多的getter/setter方法在javascript:
二传手:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/set
吸气剂:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/get
嘛,干脆不要做'Person.call(窗口,...)'。这将尝试设置'window.name'。不,这与范围无关 - 如果您引用'this'关键字,那就是调用的* context *或* receiver *。 – Bergi