[英]Using delete in JavaScript getter / setter to delete the getter / setter
This is a question about how JavaScript getters and setters work.这是一个关于 JavaScript getter 和 setter 如何工作的问题。
Mozilla's implementation of log4j as a JavaScript module (partial implementation, just the important parts needed for the intended use cases such as in Firefox Sync) contains the following getter/setter definition. Mozilla 将 log4j 实现为 JavaScript 模块(部分实现,只是预期用例所需的重要部分,例如在 Firefox Sync 中)包含以下 getter/setter 定义。
What does the 'delete' in the getter/setter do for you? getter/setter 中的“删除”对您有什么作用? What does that even mean?那有什么意思? It seems to have the effect of making the first use have different results from following uses (but if so, how)?它似乎具有使第一次使用与后续使用产生不同结果的效果(但如果是这样,如何)?
get repository() {
delete Log4Moz.repository;
Log4Moz.repository = new LoggerRepository();
return Log4Moz.repository;
},
set repository(value) {
delete Log4Moz.repository;
Log4Moz.repository = value;
},
The question (and existing answers) are missing an important piece of context;问题(和现有答案)缺少重要的上下文; the getter and setter are defined on the Log4Moz object. getter 和 setter 是在 Log4Moz 对象上定义的。 With that in mind, what happens when either the getter or setter is called and it deletes the property for which it is defined?考虑到这一点,当调用 getter 或 setter 并删除为其定义的属性时会发生什么?
delete
on accessor properties (properties with get/set) has the same effect as it does on data properties, namely that it removes the property. delete
对访问器属性(带有 get/set 的属性)具有与对数据属性相同的效果,即删除该属性。 After executing delete Log4Moz.repository
, the repository
property is no longer present on the Log4Moz
object and the getter/setter functions are no longer bound to that property.执行delete Log4Moz.repository
, repository
属性不再存在于Log4Moz
对象上,并且 getter/setter 函数不再绑定到该属性。
The next lines, which assign to Log4Moz.repository
behave as you would expect.分配给Log4Moz.repository
的下Log4Moz.repository
行为与您预期的一样。 A data property is created in the Log4Moz
object with the given value.在Log4Moz
对象中创建了Log4Moz
具有给定值的数据属性。
In effect, what this does is replace an accessor property with a data property after the first access (either get or set), making a lazily-initialized data property.实际上,这样做是在第一次访问(获取或设置)后用数据属性替换访问器属性,从而生成延迟初始化的数据属性。
FROM MDN:来自 MDN:
The delete operator deletes a property of an object. delete 运算符删除对象的属性。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete
delete operator deletes a property of an object. delete 运算符删除对象的属性。 if you have a object如果你有一个对象
o = { a : "hello", b : "world" }; o = { a : “你好”, b : “世界” };
and you do你也是
delete oa;删除oa;
your object will look like this你的对象看起来像这样
o = {b : "world" }; o = {b : "世界" };
and after that if you do之后,如果你这样做
oa = "foo"; oa = "foo";
it will add new property a to object o and assign it the value "foo" and your object will be like它将向对象 o 添加新属性 a 并为其分配值“foo”,您的对象将类似于
o = { a : "foo", b : "world" }; o = { a : "foo", b : "world" };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.