For example, an the Array
datatype has a function called pop()
, which I suppose is added using:
Array.prototype.pop = function(){ /* ... */ };
But as far as I know, the only way to make it non-enumerable is to do something like this:
Object.defineProperty(Array.prototype, "pop", { enumerable: false });
Which is not supported by all browsers.
Array.prototype.doSomething= function(){ };
var arr = [];
console.log(arr); // [doSomething: function]
So why does doSomething
show up here, while pop()
doesn't? Aren't they both added to the prototype?
When you prototype natively, you write code and build a prototype to test on real devices. Native prototyping has a goal of understanding how your product works in the real world — it's often used to validate your ideas with real users.
The objects we create using the Person constructor above have two properties: a name property, which is set in the constructor, so it appears directly on Person objects. a greet() method, which is set in the prototype.
prototype is a property of a Function object. It is the prototype of objects constructed by that function. __proto__ is an internal property of an object, pointing to its prototype.
The native prototype is a JavaScript property that all built-in constructor functions in JavaScript use to inherit methods and properties from one another.
MDN says:
A for...in loop does not iterate over non–enumerable properties. Objects created from built–in constructors like Array and Object have inherited non–enumerable properties from Object.prototype and String.prototype that are not enumerable, such as String's indexOf method or Object's toString method. The loop will iterate over all enumerable properties of the object itself and those the object inherits from its constructor's prototype (properties closer to the object in the prototype chain override prototypes' properties).
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/for...in
What is logged is supposedly coming from a for..in..
iteration, or similar.
The concept of non-enumerable properties pre-dates the feature of being to specify property enumerability right within Javascript.
MDN says:
ECMAScript 3 has an internal attribute called DontEnum. This attribute is attached to certain properties by default (§8.6.1).
The internal DontEnum attribute determines what is not to be enumerated by a for-in enumeration (§12.6.4). propertyIsEnumerable Test
https://developer.mozilla.org/en/docs/ECMAScript_DontEnum_attribute
EcmaScript 3 spec defines a lot of properties with a DontEnum
attribute. http://bclary.com/2004/11/07/
This actually does not solve the whole puzzle, because for example Array.prototype.pop
is not explicitly listed as having a DontEnum
attribute, only Array.prototype
itself is listed as so. It may be that the DontEnum
attribute of native functions is implied, but I cannot find a reference for that. The first quote from MDN does for example describe String.prototype.indexOf
as non-enumerable, while this isn't mentioned explicitly in the EcmaScript 3 spec either.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With