in

Node: Prototype Inheritance

Function construction in Node is pretty straight forward:

function someFunction(){

console.log(“hi”);

}

Functions can also be assigned to an object like so:

var greeting = function someFunction(){

console.log(“hi”);

}

You can also follow a class type pattern doing something such as:

function Car(make, model, year){

this.make = make;

this.model = model;

this.year = year;

}

var prius = new Car(‘Toyota’,’Prius’,2006)

Similari in other languages, the above instantiates the model Car with set values, creating it as an object called Prius.

We can flavor the prototype (the inheritance of the object) by adding in the prototype keyword like so:

function Car(make, model, year){

this.make = make;

this.model = model;

this.year = year;

}

Car.prototype.info = function(){

console.log(“This car is a ” + this.model + ” of this ” + this.year);

}

var prius = new Car(‘Toyota’,’Prius’,2006);

prius.info();

In the simple prototype example above, we’ve added a precondition of output to the console, when the object is created.

What do you think?

0 points
Upvote Downvote

Total votes: 0

Upvotes: 0

Upvotes percentage: 0.000000%

Downvotes: 0

Downvotes percentage: 0.000000%

Written by Admin

I work for a Telecom company writing and testing software. My passion for writing code is expressed through this blog. It's my hope that it gives hope to any and all who are self-taught.

Heart Rate Tracking with Angular2 & Fitbit

Node: Inheritance