声明和未声明变量的影响 [英] Effect of declared and undeclared variables

查看:113
本文介绍了声明和未声明变量的影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript 声明变量和未声明变量的主要区别是什么,因为删除运算符对声明的变量不起作用?

What is the major difference between JavaScript declared and undeclared variables, since the delete operator doesn't work on declared variables?

 var y = 43;     // declares a new variable
 x = 42;

 delete x;       // returns true  (x is a property of the global object and can be deleted)
 delete y;       // returns false (delete doesn't affect variable names) 

为什么会这样?全局声明的变量也是window对象的属性,为什么不能删除呢?

Why does this happen? Variables declared globally are also the properties of the window object, so why can't it be deleted?

推荐答案

已声明和未声明的全局变量

存储和访问它们的机制是相同的,但 JavaScript 在某些情况下会根据 configurable 属性(如下所述)的值对它们进行不同的处理.在常规使用中,它们的行为应该相同.

Declared and undeclared global variables

The mechanism for storing and accessing them is the same, but JavaScript treats them differently in some cases based on the value of the configurable attribute (described below). In regular usage, they should behave the same.

下面是已声明未声明全局变量的一些比较.

Below are some comparisons of declared and undeclared global variables.

var declared = 1;  // Explicit global variable (new variable)
undeclared   = 1;  // Implicit global variable (property of default global object)

window.hasOwnProperty('declared')    // true
window.hasOwnProperty('undeclared')  // true

window.propertyIsEnumerable('declared')    // true
window.propertyIsEnumerable('undeclared')  // true

window.declared     // 1
window.undeclared   // 1

window.declared   = 2;
window.undeclared = 2;

declared     // 2
undeclared   // 2

delete declared     // false
delete undeclared   // true
delete undeclared   // true (same result if delete it again)

delete window.declared     // false
delete window.undeclared   // true (same result if delete it yet again)
delete window.undeclared   // true (still true)

declaredundeclared 全局变量都是 window 对象(默认全局对象)的属性.没有一个是通过原型链从不同的对象继承的.它们都直接存在于 window 对象中(因为 window.hasOwnProperty 对两者都返回 true.

Both declared and undeclared global variables are properties of the window object (the default global object). Neither one is inherited from a different object through the prototype chain. They both exist directly in the window object (since window.hasOwnProperty returns true for both).

对于声明的全局变量,configurable 属性是false.对于未声明的全局变量,它是true.可以使用 configurable 属性的值="noreferrer">getOwnPropertyDescriptor 方法,如下图.

For declared global variables, the configurable attribute is false. For undeclared global variables, it's true. The value of the configurable attribute can be retrieved using the getOwnPropertyDescriptor method, as shown below.

var declared = 1;
undeclared = 1;

(Object.getOwnPropertyDescriptor(window, 'declared')).configurable     // false
(Object.getOwnPropertyDescriptor(window, 'undeclared')).configurable   // true

如果属性的 configurable 属性为真,则可以使用 defineProperty 方法,可以使用 delete 运算符.否则,无法更改属性,也无法以这种方式删除属性.

If the configurable attribute of a property is true, the attributes of the property can be changed using the defineProperty method, and the property can be deleted using the delete operator. Otherwise, the attributes cannot be changed, and the property cannot be deleted in this manner.

非严格模式中,delete 操作符返回 true 如果属性是可配置的,并且返回 false 如果它是不可配置的.

In non-strict mode, the delete operator returns true if the property is configurable, and returns false if it's non-configurable.

声明的全局变量

  • 是默认全局对象(window)的一个属性
  • 属性属性不能改变.
  • 不能使用delete操作符删除
  • Is a property of the default global object (window)
  • The property attributes cannot be changed.
  • Cannot be deleted using the delete operator

未声明的全局变量

  • 是默认全局对象(window)的一个属性
  • 属性属性可以改变.
  • 可以使用delete操作符删除
  • Is a property of the default global object (window)
  • The property attributes can be changed.
  • Can be deleted using the delete operator

这篇关于声明和未声明变量的影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆