javascript - Why is native datatype properties not shown on their corresponding prototypes? -
for example, array datatype has function called pop(), suppose added using:
array.prototype.pop = function(){ /* ... */ }; but far know, way make non-enumerable this:
object.defineproperty(array.prototype, "pop", { enumerable: false }); which not supported browsers.
array.prototype.dosomething= function(){ }; var arr = []; console.log(arr); // [dosomething: function] so why dosomething show here, while pop() doesn't? aren't both added prototype?
mdn says:
a for...in loop not iterate on non–enumerable properties. objects created built–in constructors array , object have inherited non–enumerable properties object.prototype , string.prototype not enumerable, such string's indexof method or object's tostring method. loop iterate on enumerable properties of object , object inherits constructor's prototype (properties closer object in prototype chain override prototypes' properties).
https://developer.mozilla.org/en-us/docs/javascript/reference/statements/for...in
what logged supposedly coming for..in.. iteration, or similar.
the concept of non-enumerable properties pre-dates feature of being specify property enumerability right within javascript.
mdn says:
ecmascript 3 has internal attribute called dontenum. attribute attached properties default (§8.6.1).
the internal dontenum attribute determines not enumerated for-in enumeration (§12.6.4). propertyisenumerable test
https://developer.mozilla.org/en/docs/ecmascript_dontenum_attribute
ecmascript 3 spec defines lot of properties dontenum attribute. http://bclary.com/2004/11/07/
this not solve whole puzzle, because example array.prototype.pop not explicitly listed having dontenum attribute, array.prototype listed so. may dontenum attribute of native functions implied, cannot find reference that. first quote mdn example describe string.prototype.indexof non-enumerable, while isn't mentioned explicitly in ecmascript 3 spec either.
Comments
Post a Comment