Inheritance example

Small Inheritance example

by jimmyt1001

JavaScript

var x = function(J){ //Every function expression is a constructor
	this.i = 0; // Setting default value for the "property" i
	this.J = J; //passing j above

	//Method
	this.getJ = function(){
		return this.J;
	};

};

//Create instances of  x using keyword new. Creat an object from it

//Two objects
var x1 = new x(1); // the first objects value of property J is one
var x2 = new x(2);// the second objects value of property J is two

// x1 and x2 are instances of x. They are two seperate objects. They inherit the properties and methods of x (shown above)
//So x is technically the parent class
console.log(x1.getJ());
console.log(x2.getJ());