Here's an object
containing a list of strings(valuesDictionnary
),
these strings are made readable from the outside
of the object with a list of getters set as a
property of the object(gettersDictionnary
).
This strange structure is used to make the strings of the list unconfigurable from the outside but configurable and so removable from the inside.
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"];
}
});
})();
Here's the point,
when a "delete" instruction is sent to one of the getters("anyValue")
from the outside of the object, it should ends up with the destruction
of the string contained in the list given by the "return" operator, not
with the destruction of the string contained in the variable gettersDictionnary
.
But it does.
Then I'm asking why in this case the "return" operator seems to give
a reference to the variable gettersDictionnary
, but not its value as it
it should do.
console.log(obj.gettersDictionnary.anyValue); //"problem"
delete obj.gettersDictionnary.anyValue;
console.log(obj.gettersDictionnary.anyValue); //"undefined"
the last console.log
should give "problem", why it doesn't ?
Here's the full code snippet :
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"