Can I move the line of "if"?

Can I move the line of “if(duck.hasOwnProperty(property)” as I think “let property in duck” means duck.hasOwnProperty(property) is right, so why we need to write it again? Anybody can Help me with this? Thanks!

1 Like

Here’s the thing: according to the MDN (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in)

The for…in statement iterates over all enumerable properties of an object that are keyed by strings (ignoring ones keyed by Symbols), including inherited enumerable properties.

What that means is, any properties coming from this one’s prototype will also be in the for...in. so how do we know which are innate and which are inherited? hasOwnProperty to the rescue!

Suppose we create a Duck class, which inherits from some Bird class, which inherits from some Animal class… The Duck will have all those inherited values, plus its own.

2 Likes

Yes, you’re right, I just found that property contains own property and prototype property, thanks!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.