Skip to content
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 : 'in' operator #1271

Open
animeshk874 opened this issue Apr 12, 2018 · 4 comments
Open

this & Object Prototypes - Chapter 3 : 'in' operator #1271

animeshk874 opened this issue Apr 12, 2018 · 4 comments

Comments

@animeshk874
Copy link

There's a paragraph related to the in operator that goes like this :

There's (currently) no built-in way to get a list of all properties which is equivalent to what the in operator test would consult (traversing all properties on the entire [[Prototype]] chain, as explained in Chapter 5). You could approximate such a utility by recursively traversing the [[Prototype]] chain of an object, and for each level, capturing the list from Object.keys(..) -- only enumerable properties.

Why do we need to capture only the enumerable properties. The in operator also checks for non-enumerable properties as well. So instead of using Object.keys(), shouldn't we use Object.getOwnPropertyNames() to build the utility?

@getify
Copy link
Owner

getify commented Apr 12, 2018

The in operator also checks for non-enumerable properties as well

No I don't think it does. See:

screen shot 2018-04-12 at 9 53 14 am

@getify
Copy link
Owner

getify commented Apr 12, 2018

Actually, sorry... I see what you mean, the in operator, not the for..in loop.

I think what I was thinking, but conflated ideas and didn't articulate well in that paragraph, is that there's no built-in way to get all the properties from the whole prototype chain (which is what both forms of in do). There's actually two different kinds of lists you might want to get: any existing property (the in test) or any enumerable property (the for..in loop).

If you wanted to get the any-property list (emulating the in operator), you're right, you'd want to use the getOwnPropertyNames(..). If you wanted to get the enumerable-property list (emulating the for..in loop), you'd want to use Object.keys(..).

I should definitely clarify this paragraph in the second edition.

@animeshk874
Copy link
Author

But that's when you are using the in operator inside a for..in loop. However, when used alone, it checks for non-enumerable properties as well. The code below prints true :

 var o = {a: 1};
 Object.defineProperty(o, "b", {value: 2, enumerable: false});

var x = Object.create(o);
"b" in x; 

@animeshk874
Copy link
Author

Yes. Exactly what I meant to say 👍

@animeshk874 animeshk874 changed the title this & Object Prototypes - Chapter 3 this & Object Prototypes - Chapter 3 : 'in' operator Apr 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants