Skip to content

Instantly share code, notes, and snippets.

@rishabhmhjn
Created May 31, 2013 06:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rishabhmhjn/5683322 to your computer and use it in GitHub Desktop.
Save rishabhmhjn/5683322 to your computer and use it in GitHub Desktop.
Javascript inheritance
var Person = function () {
this.fname = 'douglas';
this.lname = 'crockford';
};
var Student = function() {
return this;
};
Student.prototype = new Person();
var student1 = new Student();
console.log(student1 instanceof Student); // true
console.log(student1 instanceof Person); // true
console.log(typeof student1.sayMyName); // undefined
Person.prototype.sayMyName = function() {
return this.fname + " " + this.lname;
}
console.log(typeof student1.sayMyName); // function
student1.sayMyName(); // "douglas crockford"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment