Javascript inheritance – where the members creating-Collection of common programming errors

p.x

member ‘x’ firstly searched in the Point class, then in its prototype (Shape). Is it right?

Apart from that there are no “classes” in JavaScript, yes. The property name will first be searched on the p object itself, then in the object it did inherit from (there’s an internal link).

Where will be created members ‘x’ and ‘y’ – in the Point class or in Shape (prototype)?

Depends on how you create them 🙂

In a simple assignment, like p.x = 1; (which also happens in parseParametersthis[names[i]] = params[names[i]], as this === p) the property will be created on the p object.

should I actually thinking of it? Maybe there is no matter where the member created?

No, that really matters. If you would create them on the prototype object, all objects which inherit from that one would share the same values. While this is useful for the functions, it would be catastrophic for data properties like x and y which should differ from instance to instance (see also Why are my JS object properties being overwritten by other instances for an example).

Originally posted 2013-11-10 00:10:11.