4
我有一個變量中的對象的屬性的引用,是否有可能訪問該屬性所屬的對象?如果我只有一個屬性的引用,我可以到達屬性的對象嗎?
我有一個變量中的對象的屬性的引用,是否有可能訪問該屬性所屬的對象?如果我只有一個屬性的引用,我可以到達屬性的對象嗎?
var obj = {
p1: 1,
p2: function(){
return this;
},
p3: obj,
p4: function() {
return obj;
}
}
// v1 is now integer, we cannot get actual `obj` from this `v1`
var v1 = obj.p1;
// `v2()` returns `window` object (or current context object),
// so if `obj` is created only in global context (or current
// context which you're calling `v2()`), you can get reference to `obj`
var v2 = obj.p2;
// as @Ignacio mentioned, you can use `v3` as reference to `obj`
var v3 = obj.p3;
// `v4()` also reference to `obj`
var v4 = obj.p4;
除非屬性值本身包含對該對象的引用。
或者包含對可以返回對象的函數的引用。 –