Skip to content

Instantly share code, notes, and snippets.

@bcelenza
Created December 18, 2012 02:41
Show Gist options
  • Save bcelenza/4324530 to your computer and use it in GitHub Desktop.
Save bcelenza/4324530 to your computer and use it in GitHub Desktop.
// 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