[英]Why would an Object prototype method override String and Number prototype methods in JavaScript?
Trying to define a hashCode
method on Object.prototype
as well as String.prototype
and Number.prototype
. 尝试在
Object.prototype
以及String.prototype
和Number.prototype
上定义hashCode
方法。 I'm defining the prototype methods using: 我正在使用以下方法定义原型方法:
Object.defineProperty(Object.prototype, 'hashCode', {
value:function() {/*code*/},
enumerable:false
});
String.prototype.hashCode = function() {/*code*/};
Number.prototype.hashCode = function() {/*code*/};
When I create a number or string with any of ( ''
, new String()
, 3
, new Number()
), and call hashCode
on the instance, the Object.prototype.hashCode
method always runs instead of String.prototype.hashCode
or Number.prototype.hashCode
. 当我使用(
''
, new String()
, 3
, new Number()
)中的任何一个创建数字或字符串并在实例上调用hashCode
时, Object.prototype.hashCode
方法始终运行而不是String.prototype.hashCode
或Number.prototype.hashCode
。
What's wrong? 怎么了?
Make the property descriptor writable: true or it will be inherited as non-writable when writing that property on objects that inherit it. 将属性描述符设置为可写:true或在将属性写到继承该属性的对象上时将其写为不可写。 http://jsfiddle.net/5ox1a0f2 – squint
http://jsfiddle.net/5ox1a0f2 –斜视
Object.defineProperty(Object.prototype, 'hashCode', { value:function() {console.log('object')}, enumerable:false, writable:true }); String.prototype.hashCode = function() {console.log('string')}; Number.prototype.hashCode = function() {console.log('number')}; 4..hashCode()
Mixing property definitions and property assignments can lead to this kind of problems. 混合属性定义和属性分配可能导致此类问题。
It works if you also use property definition in String.prototype
and Number.prototype
: 如果您还使用
String.prototype
和Number.prototype
属性定义,则可以使用它:
Object.defineProperty(Object.prototype, 'hashCode', { value: function() {console.log('object')}, enumerable: false }); Object.defineProperty(String.prototype, 'hashCode', { value: function() {console.log('string')}, enumerable: false }); Object.defineProperty(Number.prototype, 'hashCode', { value: function() {console.log('number')}, enumerable: false }); (4).hashCode(); // "number" ('').hashCode(); // "string"
However, if you are only using property definitions because you don't want enumerability, but don't care about configurability nor writability, it may be more convenient to define the methods via assignment, and then redefine the enumerability: 但是,如果只使用属性定义是因为您不希望可枚举性,而又不关心可配置性或可写性,那么通过赋值定义方法然后重新定义可枚举性可能会更方便:
Object.prototype.hashCode = function() {console.log('object')}; String.prototype.hashCode = function() {console.log('string')}; Number.prototype.hashCode = function() {console.log('number')}; Object.defineProperty(Object.prototype, 'hashCode', {enumerable: false}); Object.defineProperty(String.prototype, 'hashCode', {enumerable: false}); Object.defineProperty(Number.prototype, 'hashCode', {enumerable: false}); (4).hashCode(); // "number" ('').hashCode(); // "string"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.