使属性不可枚举有什么好处? [英] What are the benefits of making properties non-enumerable?

查看:68
本文介绍了使属性不可枚举有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可枚举性是属性的三个属性之一:可写性,可枚举性和可配置性.我的问题是:

Enumerability is one of the three attributes of a property: writability, enumerability, and configurability. My questions are:

  • 使属性在JavaScript中不可枚举有什么好处?我知道我们通过使它们不可枚举来隐藏财产,但是财产隐藏的好处是什么?
  • 我们可以访问不可枚举的属性吗?如果是,那么使它们不可枚举有什么好处?
  • 对象的所有预定义属性都设置为不可枚举吗?例如Array的poppush属性是不可枚举的?
  • What are the benefit of making properties non-enumerable in JavaScript? I know we are hiding the property by making them non-enumerable, but what are the benefit of property hiding?
  • Can we access non-enumerable properties? If yes, then what is the benefit of making them non-enumerable?
  • Are all predefined properties of Objects set as non-enumerable? Such as the case of Array's pop and push properties being non-enumerable?

推荐答案

我认为主要好处是能够控制枚举对象属性(例如for inObject.keys())时显示的内容.

I think the main benefit is to be able to control what shows up when enumerating an object's properties, such as for in or Object.keys().

MDN用Object.defineProperty很好地说明了这一点: https ://developer.mozilla.org/zh-CN/docs/JavaScript/Reference/Global_Objects/Object/defineProperty

MDN explains it well with Object.defineProperty: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/defineProperty

因此,通常情况下,当人们想向Object中添加方法时,例如旧浏览器不支持的方法的polyfill,他们会修改.prototype.但这会使属性枚举,并且弄乱了循环/键集合中返回的内容(不使用.hasOwnProperty ...,并非所有人都使用).

So normally, when people want to add a method to Object, such as a polyfill for some method not supported in old browsers, they modify the .prototype. But that makes the property enumerable and messes up what is returned in loops/keys collection (without using .hasOwnProperty...which not everyone uses).

所以不是这样的:

Object.prototype.myMethod = function () {
    alert("Ahh");
};

您可以使用Object.defineProperty明确表示它不可枚举:

you could use Object.defineProperty to explicitly say to have it not be enumerable:

Object.defineProperty(Object.prototype, 'myMethod', {
    value: function () {
        alert("Ahh");
    },
    enumerable: false
});

这样,例如,当您使用for (var key in obj)时,"myMethod"将不会被枚举,并且您不必担心使用.hasOwnProperty.这样做的主要问题是某些浏览器当然不支持它: http://kangax .github.com/es5-compat-table/,并不是所有的库/代码都使用它,所以您不能总是依靠外部库/代码来始终正确地使用它.

That way, for example when you use for (var key in obj), "myMethod" won't be an item enumerated, and you won't have to worry about using .hasOwnProperty. The main problem with this is that some browsers don't support it of course: http://kangax.github.com/es5-compat-table/ and that not all libraries/code use it, so you can't always rely on external libraries/code to do use correctly and all the time.

您可以随时访问不可枚举的属性,这在枚举对象的属性时不会显示-这是要点.

You can access a non-enumerable property at any time you want, it just won't show up when enumerating the object's properties - that's the main point.

我确实相信对象的所有预定义"属性都是不可枚举的.这样,我实际上仅是指本机属性,不一定继承或创建.因此,以您的示例为例,poppush不会被枚举,但是如果Array.prototype.indexOf是在不支持该方法的旧版浏览器中作为Polyfill创建的,则将被枚举. ..当然可以通过使用Object.defineProperty来避免,就像我上面的示例一样.另一个示例是length属性,该属性未在上面枚举.

And I do believe that all "predefined" properties of objects are non-enumerable. By that, I really only mean native properties, not necessarily inherited or created. So with your example, pop and push will not be enumerated over, but Array.prototype.indexOf will be if it is created as a polyfill on an old browser that doesn't support that method...which of course, can be avoided by using Object.defineProperty like my example above. Another example is the length property, which is not enumerated over.

下面是一个示例: http://jsfiddle.net/aHJ3g/

Object.keys的用法和定义很重要:以给定对象自身的可枚举属性的数组形式返回数组,其顺序与for-in循环所提供的顺序相同(不同之处在于,for-in循环枚举了枚举原型链中的属性)." -来自MDN- https://developer.mozilla.org /en-US/docs/JavaScript/Reference/Global_Objects/Object/keys

The use and definition of Object.keys is important: "Returns an array of a given object's own enumerable properties, in the same order as that provided by a for-in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well)." - from MDN - https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/keys

这篇关于使属性不可枚举有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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