以单例模式引用变量的根父级

时间:2013-10-02 01:50:44

标签: javascript jquery singleton object-literal

如果我在hello.world.planet里面并且我引用了this.two,这将得到兄弟姐妹,我如何引用hello.planet而不引用变量的名称,类似于'this'

var hello = {
    world: {
        one: {
        },
        two: {
        },
        three: {
        }
    },
    planet: {
    }
}

1 个答案:

答案 0 :(得分:0)

我并没有完全关注您想要引用的位置,但您可以尝试使用.bind()。我希望这就是你要追求的目标:

var hello = {
    world: {
        one: {
        },
        two: function() {
            alert(this.planet);
        }.bind(hello),
        three: {
        }
    },
    planet: {
    }
}