已申报和未申报变量的影响 [英] Effect of declared and undeclared variables

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

问题描述

JavaScript声明变量和未声明变量之间的主要区别是什么,因为
删除操作符不适用于声明变量?

  var y = 43; //声明一个新变量
x = 42;

删除x; //返回true(x是全局对象的一个​​属性,可以删除)
delete y; //返回false(删除不影响变量名称)

为什么会发生这种情况?全局声明的变量也是窗口对象的属性,为什么它不能被删除?

解决方案

声明和未声明的全局变量



存储和访问它们的机制是相同的,但JavaScript在某些情况下会根据可配置属性(如下所述)。在常规用法中,它们应该表现相同。



全局对象都存在



以下是一些比较 undeclared 全局变量。

//显式全局变量(新变量)
undeclared = 1; //隐式全局变量(默认全局对象的属性)

window.hasOwnProperty('声明')// true
window.hasOwnProperty('undeclared')// true

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

window.declared // 1
窗口。未申报的// 1

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

声明// 2
未声明// 2

删除声明// false
删除未声明// true
删除未声明// // true(同样的结果,如果再次删除)

delete window.declared // false
delete window.undeclared // true(同样的结果,如果再次删除它)
删除窗口.undeclared // true(仍然为true)



未声明的全局变量是窗口对象(默认全局对象)的属性。原型链中的任何一个都不是通过不同的对象继承的。它们都直接存在于窗口对象中(因为 window.hasOwnProperty 返回 true / p>

可配置属性



对于声明的全局变量,可配置的属性是 false 。对于未声明的全局变量,它是 true 可配置属性的值可以使用 getOwnPropertyDescriptor 的方法,如下所示:

  var declared = 1; 
undeclared = 1;

(Object.getOwnPropertyDescriptor(window,'声明'))。可配置// false
(Object.getOwnPropertyDescriptor(window,'undeclared'))。可配置// true

如果属性的可配置属性为true,可以使用 defineProperty 方法,并且可以使用 delete 运算符。否则,属性无法更改,并且无法以此方式删除属性。



非严格模式 delete 运算符返回 true 如果属性是可配置的,并且如果它是不可配置的,则返回 false

< h2>总结

声明的全局变量


  • 是默认全局对象( window )的属性

  • 属性属性不能更改。 / li>
  • 不能使用删除运算符

$删除b
$ b

未声明的全局变量


  • 是默认全局对象( window

  • 可以更改属性
    <李>的使用删除运算符



删除 >


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) 

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

解决方案

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.

Both exist in the global object

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)

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).

The configurable attribute

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

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.

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

Summary

Declared global variable

  • Is a property of the default global object (window)
  • The property attributes cannot be changed.
  • Cannot be deleted using the delete operator

Undeclared global variable

  • Is a property of the default global object (window)
  • The property attributes can be changed.
  • Can be deleted using the delete operator

See also

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

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