Last active
August 29, 2015 14:14
-
-
Save appkr/6801beed44469f776ab1 to your computer and use it in GitHub Desktop.
JavaScript Prototypal Inheritance// source http://jsbin.com/hiteva
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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