For this part, I would like to show you about how to make inheritance in JavaScript.
For Java, you can inherit from one object to one more object by using the key word “extends” to inherit all the properties of the parent object.

For JavaScript, we can make inheritance that is similar to the static language like Java also.
Now I would like to choose one trick to show here is about “prototypical inheritance”.

Here try it out with fire bug:

function Person(name, age) { 
	    if (Boolean(name)) 
	        this.name = name; 
	    if (Boolean(age)) 
	        this.age = age; 
} 

// Getters 
Person.prototype.getName = function () { 
        return this.name; 
} 
Person.prototype.getAge = function () { 
         return this.age; 
} 
	 
// Setters 
Person.prototype.setName = function (name) { 
          this.name = name; 
} 
Person.prototype.setAge = function (age) { 
           this.age = age; 
} 
	 
// A default name for Person objects that don't have a name 
Person.prototype.name = "John Doe"; 

var jane = new Person("Jane Smith", 35); 

var noName = new Person(); 
noName.setAge(29); 
Advertisements