Created
June 17, 2012 01:26
-
-
Save fiveisprime/2943077 to your computer and use it in GitHub Desktop.
Object.create inheritance example
This file contains hidden or 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 employee = Object.create(matt, { | |
| id: { | |
| value: '1234', | |
| writable: false | |
| }, | |
| credentials: { | |
| get: function() { | |
| return this.introduce() + " and my ID is: " + this.id; | |
| } | |
| } | |
| }); | |
| console.log(employee.credentials); |
This file contains hidden or 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 employee = Object.create(matt); |
This file contains hidden or 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 employee = Object.create(matt, { | |
| id: { | |
| value: '1234' | |
| }, | |
| getCredentials: { | |
| value: function() { | |
| return this.introduce() + " and my ID is: " + this.id; | |
| } | |
| } | |
| }); | |
| console.log(employee.getCredentials()); |
This file contains hidden or 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
| function Person(name) { | |
| this.name = name; | |
| } | |
| Person.prototype.introduce = function() { | |
| return "Hi, I'm " + this.name; | |
| }; | |
| var matt = new Person('Matt'); | |
| console.log(matt.introduce()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment