鉴于以下我如何使其正常工作?
return {
methodA: function() {
return methodB();
},
methodB: function() {
alert("hi");
}
}
Run Code Online (Sandbox Code Playgroud)
您需要通过引用上下文this.
var foo = {
methodA: function() {
return this.methodB(); //context
},
methodB: function() {
alert("hi");
}
}
foo.methodA(); //alerts "hi"
Run Code Online (Sandbox Code Playgroud)