为什么Object.keys不是Object.prototype.keys? [英] Why is Object.keys not Object.prototype.keys?

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

问题描述

我想知道为什么规范定义了 Object.keys 而不是 Object.prototype.keys ?如果API一致,肯定会好得多,我想理解为什么不是这样。

I'm wondering why the spec has defined Object.keys instead of Object.prototype.keys? it would surely be much better if the API was consistent, and I would like to understand why that is not the case.

其他方法在 Object.prototype ,为什么不是这种情况?

Other methods are on Object.prototype, so why is this not the case?

推荐答案

因为你可以轻易地覆盖它通过使用名为 keys 的键来定义您的对象,持有一个值或一个函数。

Because you could easily override that by defining your object with a key called keys holding a value or a function.

var myObj = {
  val1: 'hi',
  keys: function() {
    return true;
  }
};

您希望 myObj.keys()返回什么

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

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