Javascript中未定义和未定义之间的区别 [英] Difference between undefined and not being defined in Javascript

查看:184
本文介绍了Javascript中未定义和未定义之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅 http://jsfiddle.net/FDhQF/1/ 了解一个简单的例子。

See http://jsfiddle.net/FDhQF/1/ for a trivial example.

未定义的内容与Javascript中未定义的内容之间有什么区别?例如,尝试访问未定义的对象的属性(实际上,尝试访问变量)将返回 undefined 。但你也可以设置 = undefined 。当你这样做,尝试访问它仍然返回undefined,但指针仍然存在。如上所示,一个例子是如何迭代对象仍然遍历您已经(重新)声明为未定义的属性。看起来有两种不同的未定义。任何人都可以对这种情况有所了解吗?

What's the difference between something being undefined and something not being defined in Javascript? For instance, trying to access a property for an object (effectively, trying to access a variable) that isn't defined will return undefined. But you can also set something = undefined. When you do that, trying to access it still return undefined, but the pointer is still there. An example, as above, is how iterating over an object still goes over the property that you've (re)declared as undefined. It seems like there are two different sorts of undefined. Can anyone shed some light on the situation?

推荐答案

两者都访问未在对象和属性上定义的属性包含原始未定义的值,将返回 undefined

Both, accessing a property that isn't defined on an object and a property that contains the primitive undefined value, will return you undefined.

例如:

var obj = {
  a: undefined
};

obj.a; // undefined
obj.b; // undefined

区别在于 a 是一个自己的财产, b 不是:

The difference is that a is an own property, and b isn't:

obj.hasOwnProperty('a'); // true
obj.hasOwnProperty('b'); // false

在第一种情况下 a 是一个自己的属性,即使它包含 undefined 作为其值。在第二种情况下, b 不是自己的属性,访问 obj.b 将查找名为<$的属性c $ c> b ,在原型链中一直向上。

In the first case a is an own property, even if it contains undefined as its value. In the second case, b is not an own property, accessing obj.b will look for a property named b, all way up in the prototype chain.

当原型链结束时(当它到达带有<的对象时) code> null [[Prototype]] ),属性查找结束, undefined

When the prototype chain ends (when it reaches an object with a null [[Prototype]]), the property lookup ends and undefined is explicitly returned.

您应该知道 hasOwnProperty 方法只检查对象是否物理存在于对象上(自己的属性),但是我们还有继承的属性,对于这种情况,我们可以使用运营商中的noreferrer> ,例如:

You should know that the hasOwnProperty method checks only if the property physically exist on the object (own properties), but we have also inherited properties, for that case we can use the in operator, for example:

function Test () {}
Test.prototype.a = 'foo'; // instances of Test will inherit from Test.prototype

var obj = new Test(); // { a="foo",  b="bar"}
obj.b = 'bar';

obj.hasOwnProperty('a');  // false
'a' in obj;               // true
obj.a;                    // 'foo'

obj.hasOwnProperty('b');  // true

如您所见, obj 继承自 Test.prototype ,而 a 属性不是自己的属性,但它可以通过原型链获得。这就是为什么 hasOwnProperty 返回 false 并且运算符中的返回 true

As you can see, obj inherits from Test.prototype, and the a property is not an own property, but it is available through the prototype chain. That's why hasOwnProperty returns false and the in operator returns true.

您可以看到 [[Get]] 内部操作

You can see how internally properties are resolved by the [[Get]] internal operation

注意:


  • 访问 undefined 作为标识符在ECMAScript 3(最广泛实现的语言标准版本)上不被认为是安全的,因为它不是语言关键字(例如 null 只是全局对象的一个​​属性,它可以在这个版本的Spec上写入,这意味着如果有人替换它的值(例如 window.undefined ='LOL '; ),它会破坏你的代码。

  • Accessing undefined as an identifier is not considered to be safe on ECMAScript 3 (the most widely implemented version of the language standard), because instead of being a language keyword (such as null for example) is just a property of the global object, and it is writable on this version of the Spec., meaning that if someone replaces its value (e.g. window.undefined = 'LOL';) , it will break your code.

typeof operator ,例如:

The typeof operator as @strager mentions can be used instead, for example:

if (typeof obj.prop == 'undefined') { /*....*/ }

此运算符始终返回一个字符串(使用 == :)是安全的,并且它的值取决于其操作数的类型,可能的值在此处

This operator returns always a string (it's safe to use == :), and its value depends of the type of its operand, the possible values are described here.

解决此问题的另一种常见方法是声明自己的 undefined 变量,该变量可在你的函数,例如,一些库使用以下模式:

Another common way to solve this is to declare your own undefined variable, available on the scope of your functions, for example, some libraries use the following pattern:

(function(undefined) {
  // code here
})();

该函数有一个名为 undefined 的参数,并且它立即执行而不传递任何值(最后一对或者parens进行调用)。

The function has an argument named undefined, and it is executed immediately without passing any value to it (the last pair or parens make the invocation).

也许值得一提的是 undefined 全局属性最终在ECMAScript 5中描述作为不可写(不可变,以及不可枚举和不可配置 - 不可删除 - )。

Maybe is worth mentioning that the undefined global property was finally described on ECMAScript 5 as non-writable (immutable, as well non-enumerable and non-configurable -non deletable-).


  • 使用<直接来自对象实例的code> hasOwnProperty 方法也不被视为 safe ,因为如果某个对象具有相同名称的属性,则原始方法将是阴影。例如:

  • Using the hasOwnProperty method directly from an object instance is also not considered as safe, because if some object has a property with the same name, the original method will be shadowed. For example:

var obj = { hasOwnProperty: function () { /* evil code :) */ } };


如果您致电:

    obj.hasOwnProperty('prop'); 

对象上定义的方法将被执行(你不会想要这个,因为你确切地知道你要调用哪个方法...),因为 shadow 来自 Object.prototype 的方法,但是可以通过以下方式安全地调用它:

The method defined on the object will be executed (and you wouldn't want this since you know exactly which method you want to invoke...), because it shadows the one from the Object.prototype, however it can be safely invoked by:

    Object.prototype.hasOwnProperty.call(obj, 'prop');

这篇关于Javascript中未定义和未定义之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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