-
Notifications
You must be signed in to change notification settings - Fork 33.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
this & Object Prototypes - Chapter 3: Symbol.iterator enumerablility #1388
Comments
This detail in the spec changed after publication of the book, which predated the spec by more than a year. |
@Beaglefoot Did you get the answer? According to MDN, Object.getOwnPropertyDescriptor(Symbol, 'iterator');
// {value: Symbol(Symbol.iterator), writable: false, enumerable: false, configurable: false} You misunderstood its meaning. That doesn't mean that var object = {
[Symbol.iterator]: function() {}
};
Object.getOwnPropertyDescriptor(object, Symbol.iterator);
// {value: ƒ, writable: true, enumerable: true, configurable: true}
object.propertyIsEnumerable(Symbol.iterator); // true (of course).
// What about to change its `enumerable`?
Object.defineProperty(object, Symbol.iterator, {
enumerable: false
});
object.propertyIsEnumerable(Symbol.iterator); // false It is helpful to think that an object uses But, why the
Object.defineProperty(object, Symbol.iterator, {
enumerable: true
});
object.propertyIsEnumerable(Symbol.iterator); // true
for (let prop in object) {
console.log(prop);
} // nothing printed. |
Upd: seems I am a little bit late with an answer... But here it is anyway. @jinbeomhong Ok, I may misinterpret this, by here's my explanation. First of all, by default ({ a: 1 }).propertyIsEnumerable('a'); // true The same is true for user defined symbol properties. const mySym = Symbol('js is weird');
({ [mySym]: 1 }).propertyIsEnumerable(mySym); // true Arrays, strings and other built-in iterables have predefined ([]).propertyIsEnumerable(Symbol.iterator); // false
('').propertyIsEnumerable(Symbol.iterator); // false But if you try to redefine this property, then default property descriptor rules will take place. var arr = [];
arr[Symbol.iterator] = () => {};
(arr).propertyIsEnumerable(Symbol.iterator); // true The second part is actual traverse over properties. And spec says:
So that's why we don't see these enumerable properties in |
In the end of the chapter there's an example and statement:
I'm somewhat confused by the part:
MDN states that
Symbol.iterator
is non-enumerable, my own experimentation is a little bit confusing...Is there something else we should know?
The text was updated successfully, but these errors were encountered: