Skip to content

Instantly share code, notes, and snippets.

@appkr
Last active August 29, 2015 14:14
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 appkr/6801beed44469f776ab1 to your computer and use it in GitHub Desktop.
Save appkr/6801beed44469f776ab1 to your computer and use it in GitHub Desktop.
JavaScript Prototypal Inheritance// source http://jsbin.com/hiteva
var Person = function(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
};
Object.defineProperties(Person.prototype, {
sayHi : {
value : function () {
return "Hi there";
},
enumerable : true
},
fullName : {
get : function () {
return this.firstName + " " + lastName;
},
enumerable : true
}
});
var Employee = function(firstName, lastName, position) {
Person.call(this, firstName, lastName);
this.position = position;
};
Employee.prototype = Object.crate(Person.prototype, {
sayHi : {
value : function () {
return Person.protytype.sayHi.call(this) + " My name is " + this.fullName;
},
enumerable : true
},
fullName : {
get : (function () {
var desc = Object.getOwnPropertyDescriptor(Person.prototype, "fullName").get;
return function () {
return desc.call(this) + ", " + this.position;
};
})(),
enumerable : true
}
});
var johnDoe = new Employee("John", "Doe", "Manager");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment