gpt4 book ai didi

javascript - 无法理解在 JavaScript 中删除 vars 的行为

转载 作者:搜寻专家 更新时间:2023-11-01 05:23:25 25 4
gpt4 key购买 nike

问题是:

var x = 5;
window.x === x // true. x, as it seems, is a property of window
delete x; // false
delete window.x; // false;

但是

window.x = 5;
delete window.x; // true

window.x = 5;
delete x; // true

如何解释这种行为?

最佳答案

本质上,原因是声明的变量是使用内部 DontDelete 属性创建的,而通过赋值创建的属性则不是。

这是一篇很好的文章,解释了delete 的内部细节:Understanding delete

When declared variables and functions become properties of a Variable object — either Activation object (for Function code), or Global object (for Global code), these properties are created with DontDelete attribute. However, any explicit (or implicit) property assignment creates property without DontDelete attribute. And this is essentialy why we can delete some properties, but not others:

关于javascript - 无法理解在 JavaScript 中删除 vars 的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21134578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com