prototypal inheritance concept in javascript as a prototype based language

Classical inheritance is about extending types of things. Say you have a class, like Bike. When you want extend the behaviour, you have to design a new type of bike (like MotorBike). It’s like building a factory – you make lots of blueprints, and blueprints that reference those blueprints, but in order to ride one … Read more

[[Prototype]] vs prototype: ..what is the difference? (MyCons.__proto__ === MyCons.prototype) equals FALSE

Think of it like this. MyConstructor is a function object, so it was created by Function; therefore its [[Prototype]] (or __proto__) is identical to Function.prototype. In the same way, var myObj = new MyConstructor() creates an object myObj with a [[Prototype]] identical to MyConstructor.prototype. To put it another way, functions have a prototype property, and … Read more

Javascript inheritance: call super-constructor or use prototype chain?

The answer to the real question is that you need to do both: Setting the prototype to an instance of the parent initializes the prototype chain (inheritance), this is done only once (since the prototype object is shared). Calling the parent’s constructor initializes the object itself, this is done with every instantiation (you can pass … Read more

JavaScript instance functions versus prototype functions [duplicate]

You can actually add another level of privilege via wrapping the whole thing in a self-executing function: var MyObj = (function() { // scoping var privateSharedVar=”foo”; function privateSharedFunction() { // has access to privateSharedVar // may also access publicSharedVar via explicit MyObj.prototype // can’t be called via this } function MyObj() { // constructor var … Read more

What does it mean that Javascript is a prototype based language?

Prototypal inheritance is a form of object-oriented code reuse. Javascript is one of the only [mainstream] object-oriented languages to use prototypal inheritance. Almost all other object-oriented languages are classical. In classical inheritance, the programmer writes a class, which defines an object. Multiple objects can be instantiated from the same class, so you have code in … Read more

Understanding prototypal inheritance in JavaScript

To add to Norbert Hartl’s answer, SuperCar.prototype.constructor isn’t needed, but some people use it as a convenient way of getting the constructing function of an object (SuperCar objects in this case). Just from the first example, Car.call(this, name) is in the SuperCar constructor function because when you do this: var mySuperCar = new SuperCar(“SuperCar”); This … Read more