var O = {
elements: {
main: function() { return jQuery("#main"); },
footer: function() { return jQuery("#footer"); }
},
main: function(html) {
return (this.elements.main());
},
style: {
setMaincolor: function() {
// TypeError: Cannot call method 'main' of undefined
return (this.elements.main());
}
}
};
所以;我是 O.style Object parents Objects ????
O.style.setMaincolor() // TypeError: Cannot call method 'main' of undefined
O.main() // [<div id="main"></div>]
setMaincolor 方法返回 O 对象
最佳答案
this
引用调用方法的对象。
main()
是在 O
对象上调用的,所以 this
是对 O
的引用。因此 this.elements === O.elements
。
setMaincolor()
是在 style
对象上调用的,因此 this
将是对 O.style
的引用>,它没有 .elements
属性。
关于javascript - OOP Javascript父对象方法还是对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12624173/