Object.prototype
あなた自身のプロパティがそれらをシャドウする可能性があるので、彼らはあまり多くのプロパティを望んでいないと思います。
それらが含まれるほど、競合の可能性が高くなります。
keys
にあった場合、このオブジェクトのキーを取得するのは非常に不器用ですprototype
...
var myObj: {
keys: ["j498fhfhdl89", "1084jnmzbhgi84", "jf03jbbop021gd"]
};
var keys = Object.prototype.keys.call(myObj);
シャドウされる可能性のあるプロパティを導入すると、コードが破損する可能性がある例。
に新しいプロパティを追加することがなぜ重要なのかについては、多少の混乱があるようObject.prototype
です。
このように見えるコードが存在することを想像するのはまったく難しいことではありません...
if (someObject.keys) {
someObject.keys.push("new value")
else
someObject.keys = ["initial value"]
Clearly this code would break if you add a keys
function to Object.prototype
. The fact that someObject.keys
would now be a shadowing property breaks the code that is written to assume that it is not a shadowing property.
Hindsight is 20/20
If you're wondering why keys
wasn't part of the original language, so that people would at least be accustomed to coding around it... well I guess they didn't find it necessary, or simply didn't think of it.
There are many possible methods and syntax features that aren't included in the language. That's why we have revisions to the specification, in order to add new features. For example, Array.prototype.forEach
is a late addition. But they could add it to Array.prototype
, because it doesn't break proper uses of Array
.
It's not a realistic expectation that a language should include every possible feature in its 1.0
release.
Since Object.keys
does nothing more than return an Array of an Object's enumerable own properties, it's a non-essential addition, that could be achieved with existing language features. It should be no surprise that it wasn't present earlier.
Conclusion
Adding keys
to Object.prototype
most certainly would break legacy code.
In a tremendously popular language like JavaScript, backward compatibility is most certainly going to be an important consideration. Adding new properties to Object.prototype
at this point could prove to be disastrous.