关于 JSON 的一般问题,假设我有:
var foo = {
a: null,
init: function() {
this.a = document.getElementById(...);
}
}
这将给出一个带有 null 的值。除了通过 foo.a = document.getElementById(...) 在外部执行之外,还有什么办法可以超出此范围并获取文档元素?
编辑:更恰本地更改标题。
我正在调用 foo.init(),但是当我这样做时,说我有:
然后我调用包含 this.a = document.getElementById('test') 的 init.foo() ,它似乎返回 NULL,也许我做错了什么。
最佳答案
这工作正常:
var foo = {
a: null,
init: function() {
this.a = document.getElementById('test');
}
}
foo.init();
console.log(foo.a);
关于javascript - JavaScript 对象的范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5053965/