为什么 Object.keys() 方法没有添加到 Object.prototype? [英] Why is Object.keys() method not added to Object.prototype?

查看:64
本文介绍了为什么 Object.keys() 方法没有添加到 Object.prototype?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 javascript 以某种方式工作的原因感到困惑.如果我有一个对象设置为 obj 的变量.

I am confused about why javascript works in a certain way. If I have an Object set to the variable of obj.

如果我想列出对象中的所有键.
我会说

If I wanted to list all the keys in the object.
I would say

Object.keys(obj) 

为什么不是

obj.keys()

如果我正在处理一个数组,它将是 arr.pop()那么为什么 obj.keys() 的语法不一样呢??同样为什么它必须是 Object.keys(obj)

If I was working with an Array it would be arr.pop() So why not the same syntax for an obj.keys()?? Again why does it have to be Object.keys(obj)

对不起,我只是想学习语言的特殊性

Sorry for the dumb question I am just trying to learn the particularities of the language

推荐答案

可能Object.prototype 上放置一个 keys 方法,所以事情如您所愿,但这不是一个好主意.

It's possible to put a keys method on Object.prototype so that things work as you're expecting, but it's not a good idea.

Object.prototype.keys = function() {
  return Object.keys(this);
};

const obj = {
  prop: 'val'
};
const keys = obj.keys();
console.log(keys);

对于对象键,问题在于对象通常可以具有任何类型的键.该对象甚至可能有一个名为 keys 的键.因此,例如:

With object keys, the issue is that objects can generally have any sort of keys. The object may even have a key named keys. So, for example:

const obj = {
  name: 'building 1',
  keys: 'foo'
}

这里,如果你做了obj.keys(),你会得到一个TypeError,因为keys属性直接引用了这个属性在对象上,而不是 Object.prototype 方法.

Here, if you did obj.keys(), you would get a TypeError, because the keys property refers to the property directly on the object, rather than the Object.prototype method.

Object.prototype.keys = function() {
  return Object.keys(this);
};

const obj = {
  name: 'building 1',
  keys: 'foo'
};


const keys = obj.keys();
console.log(keys);

因此,用于获取对象所有键的方法被放在 Object 上,作为静态方法,而不是 Object.prototype 上的原型方法,以避免可能的名称冲突.

So, the method to use to get all keys of an object was put on Object instead, as a static method, rather than a prototype method on Object.prototype, so as to avoid possible name collisions.

另一方面,几乎普遍期望数组具有某些特定于数组的方法(如push),并且没有其他/em>.数组不是通用对象——您几乎总是希望数组只有数组方法,而数组几乎从不添加任意键.(如果您看到具有此功能的代码,那么它可能是值得重构的代码.)因此,对于数组,数组方法不可能像泛型对象那样导致名称冲突.

Arrays, on the other hand, are pretty much universally expected to have a certain few methods particular to arrays (like push), and nothing else. Arrays are not generic objects - you'd pretty much always expect an array to have only array methods, and arrays almost never get arbitrary keys added to them. (If you see code that has this, it's probably code that deserves refactoring.) So, with arrays, there's no real possibility of an array method causing name collisions, like there was with a generic object.

因此,拥有 Array.prototype.push 没有坏处,但 Object.prototype.keys 很容易引起问题.

Thus, there's no harm in having Array.prototype.push, but Object.prototype.keys could easily cause problems.

这篇关于为什么 Object.keys() 方法没有添加到 Object.prototype?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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