Constructor / Prototype

function SimplePerson(name, age) {
  this.name = name;
  this.age = age;
}
 
SimplePerson.prototype.talk = function(greet) {
  greet = greet || "Hi";
  console.log(greet + " I, " + this.name + ", am " + this.age + " years.");
};
 
var simplePerson = new SimplePerson("Jen", 26);
var anotherSimplePerson = new SimplePerson("Joe", 42);
 
console.log(typeof simplePerson); //Object
console.log(simplePerson.constructor); //simpleperson
 
simplePerson.talk();//"Hi I, Jen, am 26 years."
anotherSimplePerson.talk("yo!"); // "yo! I, Joe, am 42 years."

Add your comment