Skip to content

Instantly share code, notes, and snippets.

@imayobrown
Created April 9, 2015 15:39
Show Gist options
  • Save imayobrown/235d429f73101cd861c5 to your computer and use it in GitHub Desktop.
Save imayobrown/235d429f73101cd861c5 to your computer and use it in GitHub Desktop.
Javascript prototype and object examination
//Here we define Person object by creating a constructor for it. We could also do this by doing new Object() then dynamically
//adding attributes and functions to it.
var Person = function(name, age){
this.name = name;
this.age = age;
this.getAge = function(){
return this.age;
};
this.toString = function(){return "Person";};
};
//Creates John as a object of type Person. John has no prototype because he has no constructor function. He is simply an
//object of type Person. If one changes/modifies Person's prototype the changes will be propogated to John.
var John = new Person("John");
//Create customer object type and set its prototype to Person with same name and age. This means that any changes to Person
//object type should propogate to Customer.
var Customer = function(name, age){
this.name = name;
this.age = age;
this.getAge = function(){return age;};
};
//Set Customer Prototype.
Customer.prototype = new Person();
//Create Ellen as an object of type Customer.
var Ellen = new Customer("Ellen","21");
alert('John prototype: '+John.prototype.toString()));
alert('Ellen prototype: '+Ellen.prototype.toString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment