如果我这样做:
Object.defineProperty(window, 'hello',{
get: ()=>"hello to you!"
});
调用方式为:
你好
并回复 向你问好! 如何删除它?
最佳答案
您需要向您在defineProperty
中传递的描述符对象添加另一个属性,即configurable: true
,之后您可以使用删除
运算符。
'use strict'
Object.defineProperty(window, 'hello',{
get: ()=>"hello to you!",
configurable: true //make this true
});
console.log(window.hello);
delete window.hello
// hello is deleted from window
console.log(window.hello);
默认情况下,如果您没有将描述符对象的configurable
设置为true
,则它为 false,来自 docs :
configurable
true if and only if the type of this property descriptor may be changed and if the property may be deleted from the corresponding object. Defaults to false.
关于javascript - 如何删除使用 Object.defineProperty 定义的窗口 getter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57452233/