使用Javascript:为什么Object.keys(someobject),而不是someobject.keys? [英] Javascript: why Object.keys(someobject), rather than someobject.keys?

查看:1803
本文介绍了使用Javascript:为什么Object.keys(someobject),而不是someobject.keys?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常用得到一个对象键数组:

I frequently get an array of an objects keys using:

Object.keys(someobject)

我很舒服这样做。据我所知,对象是对象的构造函数和键()是它的一个方法,而且键()将返回在任何对象被赋予作为第一个参数键的列表。我的问题不是如何获取对象的钥匙 - 请不要与非的答案解释这个答复。

I'm comfortable doing this. I understand that Object is the Object constructor function, and keys() is a method of it, and that keys() will return a list of keys on whatever object is given as the first parameter. My question is not how to get the keys of an object - please do not reply with non-answers explaining this.

我的问题是,为什么没有更predictable键()或getKeys()方法,或钥匙实例变量上的Object.prototype 可用,所以我可以有:

My question is, why isn't there a more predictable keys() or getKeys() method, or keys instance variable available on Object.prototype, so I can have:

someobject.keys()

或作为一个实例变量:

someobject.keys

和返回键阵列?

此外,我的本意是要了解的Javascript的设计,以及获取钥匙的有些直观的机制,用于什么目的。我不需要帮助,得到钥匙。

Again, my intention is to understand the design of Javascript, and what purpose the somewhat unintuitive mechanism of fetching keys serves. I don't need help getting keys.

推荐答案

我想回答你的问题是因为委员会决定,而现在我已经能听到你问为什么?这句话结束前。

I guess an answer to your question is "Because the committee decided so", but I can already hear you ask "why?" before the end of this sentence.

我最近看了这个,但现在我无法找到源头。它归结为是,在很多情况下,你必须使用 Object.prototype.keys.call(为MyObject)无论如何,因为的可能性为MyObject 。键已经在该对象使用别的东西。

I read about this recently but I can't find the source right now. What it boiled down to was that in many cases you had to use Object.prototype.keys.call(myobject) anyway, because the likelihood of myobject.keys already being used in the object for something else.

我想你会发现这个归档邮件线程有趣的,其中例如布兰登·艾克的讨论的ECMAScript 5的新方法的某些方面

I think you will find this archived mail thread interesting, where for example Brendan Eich discuss some aspects of the new methods in ECMAScript 5.

更新:
而在邮件归档我发现的这个

主题:应该Object.keys被重新定位为Object.prototype.keys

Topic: Should Object.keys be repositioned as Object.prototype.keys

讨论:艾伦认为,这是不是一个真正的元层操作
  因为它是用于在应用层code使用作为替代
  到的for..in获取枚举属性名称的列表。作为一个
  应用层的方法,它属于Object.prototype上,而不是
  Object构造。有原则普遍同意,但
  它是务实由Doug和马克认为,这是极有可能
  一个用户定义的对象将定义一个名为自有物业钥匙
  这将影Object.prototype.keys使其无法进入
  使用这样的对象。

Discussion: Allen argued that this isn't really a meta layer operation as it is intended for use in application layer code as an alternative to for..in for getting a list of enumerable property names. As a application layer method it belongs on Object.prototype rather than on the Object constructor. There was general agreement in principle, but it was pragmatically argued by Doug and Mark that it is too likely that a user defined object would define its own property named "keys" which would shadow Object.prototype.keys making it inaccessible for use on such objects.

动作:保持它作为Object.keys

这篇关于使用Javascript:为什么Object.keys(someobject),而不是someobject.keys?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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