ES6 – declare a prototype method on a class with an import statement

You can still attach a method on a class‘ prototype; after-all, classes are just syntactic sugar over a “functional object”, which is the old way of using a function to construct objects. Since you want to use ES6, I’ll use an ES6 import. Minimal effort, using the prototype: import getColor from ‘path/to/module’; class Car { … Read more

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

What does the new keyword do under the hood?

Quoting Douglas Crockford from the Good Parts book (page 47), to answer the title of this question: If the new operator were a method instead of an operator, it could be implemented like this: Function.method(‘new’, function () { // Create a new object that inherits from the // constructor’s prototype. var that = Object.create(this.prototype); // … Read more