JavaScript无法访问兄弟方法

Mar*_*ace 3 javascript

鉴于以下我如何使其正常工作?

return {

    methodA: function() {
        return methodB();
    },
    methodB: function() {
        alert("hi");
    }
}
Run Code Online (Sandbox Code Playgroud)

Utk*_*nos 5

您需要通过引用上下文this.

var foo = {

    methodA: function() {
        return this.methodB(); //context
    },
    methodB: function() {
        alert("hi");
    }
}

foo.methodA(); //alerts "hi"
Run Code Online (Sandbox Code Playgroud)