Created
December 18, 2012 02:41
-
-
Save bcelenza/4324530 to your computer and use it in GitHub Desktop.
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
// create the base task object | |
var task = { | |
getName: function() { | |
return "Frank"; | |
} | |
} | |
// now create a new timedTask object based on the task object | |
var timedTask = Object.create(task); | |
// specify any differences between timedTask and task. | |
// This is actually called 'differencial inheritance', for the curious | |
timedTask.getDuration = function() { | |
return 42; | |
} | |
// do some tests | |
console.log(task.getName()); // returns 'Frank' | |
console.log(task.getDuration()); // ERROR: getDuration undefined | |
console.log(timedTask.getName()); // returns 'Frank' | |
console.log(timedTask.getDuration()); // returns 42 | |
// create more objects! | |
var myTimedTask1 = Object.create(timedTask); | |
// override the duration for timed task 1 | |
myTimedTask1.getDuration = function() { | |
return 0; | |
} | |
var myTimedTask2 = Object.create(timedTask); | |
var myTimedTask3 = Object.create(timedTask); | |
var myTimedTask4 = Object.create(timedTask); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment