【发布时间】:2015-10-08 22:12:41
【问题描述】:
这是一个对象
包含字符串列表(valuesDictionnary),
这些字符串可以从外部读取
对象的 getter 列表设置为
对象的属性(gettersDictionnary)。
这个奇怪的结构是用来制作 列表从外部无法配置,但可配置,因此可从 里面。
var obj = new (function () {
var gettersDictionnary = {},
valuesDictionnary = {
anyValue: "problem"
};
Object.defineProperty(this, "gettersDictionnary", {
configurable: false,
get: function () {
return gettersDictionnary;
}
});
Object.defineProperty(gettersDictionnary, "anyValue", {
configurable: true,
get: function () {
return valuesDictionnary["anyValue"];
}
});
})();
重点是,
当“删除”指令发送到其中一个 getter("anyValue")
从对象的外部,它应该以破坏结束
“return”运算符给出的列表中包含的字符串,而不是
随着变量gettersDictionnary中包含的字符串的破坏。
但确实如此。
然后我问为什么在这种情况下“返回”运算符似乎给出
对变量 gettersDictionnary 的引用,但不是它的值
应该可以的。
console.log(obj.gettersDictionnary.anyValue); //"problem"
delete obj.gettersDictionnary.anyValue;
console.log(obj.gettersDictionnary.anyValue); //"undefined"
最后一个console.log 应该给出“问题”,为什么没有?
这里是完整的代码 sn-p:
var obj = new (function () {
var gettersDictionnary = {},
valuesDictionnary = {
anyValue: "problem"
};
Object.defineProperty(this, "gettersDictionnary", {
configurable: false,
get: function () {
return gettersDictionnary;
}
});
Object.defineProperty(gettersDictionnary, "anyValue", {
configurable: true,
get: function () {
return valuesDictionnary["anyValue"];
}
});
})();
console.log(obj.gettersDictionnary.anyValue); //"problem"
delete obj.gettersDictionnary.anyValue;
console.log(obj.gettersDictionnary.anyValue); //"undefined"
【问题讨论】:
标签: javascript pass-by-reference getter defineproperty