Skip to content

Instantly share code, notes, and snippets.

@mbilokonsky
Created December 18, 2012 06:03
Show Gist options
  • Save mbilokonsky/4325454 to your computer and use it in GitHub Desktop.
Save mbilokonsky/4325454 to your computer and use it in GitHub Desktop.
sample composition OOP in JS
function Task(name) {
var self = this;
self.getName = function() { return name; }
self.getAsigner = function() { return 'Frank'; }
self.getAssignee = function() { return 'Joe'; }
self.foo = function() { return 'bar'; }
}
function TimedTask(name, startTime) {
var self = this;
var task = new Task(name);
// bring over any functions exposed by the task
for (key in task) {
self[key] = task[key];
}
self.getDuration = function() { return Date.now - startTime; }
self.foo = function() { return 'bang'; } // override task.foo
}
var task1 = new TimedTask("task1", Date.now());
var task2 = new TimedTask("task2", Date.now());
@Krxtopher
Copy link

Thanks for the example. You're example is essentially doing the same thing as assigning a prototype to establish inheritence, so if this syntax feels more comfortable then continue to use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment