gpt4 book ai didi

javascript - 在 JavaScript 中,如果 "delete"一个不存在的属性会发生什么?

转载 作者:IT王子 更新时间:2023-10-29 03:15:56 26 4
gpt4 key购买 nike

如果我有一个变量,在 JavaScript 中会发生什么,比如:

var exampleObject = {one:'foo',two:'bar'};

然后我删除了一个不存在的属性,a la:

delete exampleObject.seven;

是否存在随处发生的标准操作过程(无、错误消息、脚本崩溃等),或者这是否取决于某种实现(浏览器引擎等)?

最佳答案

没有任何反应。

假设,x = {}Type(x.y) 不是 Reference Specifcation Type (不能有对不存在的属性的“引用”)。根据11.4.1 The delete Operator ,这满足规则:

  1. Let ref be the result of evaluating UnaryExpression.
  2. If Type(ref) is not Reference, return true.
  3. ...

这种行为(“不采取行动”)已经存在了很长时间 - 任何行为不同的环境都是不合规的。来自3rd Edition ECMAScript Specification :

When the [[Delete]] method of O is called with property name P, the following steps are taken:

  1. If O doesn’t have a property with name P, return true.
  2. ..

关于javascript - 在 JavaScript 中,如果 "delete"一个不存在的属性会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16406784/

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