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

Referencing root parent of variable in singleton pattern

本文关键字:变量 单例模式 引用      更新时间:2023-09-26

如果我在hello.world.planet中并且我引用this.two,那将得到兄弟姐妹,我如何在不引用变量名称的情况下引用hello.planet,类似于"this"

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

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

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