Skip to content

Instantly share code, notes, and snippets.

@bgerrissen
Created July 1, 2011 13:59
Show Gist options
  • Save bgerrissen/1058592 to your computer and use it in GitHub Desktop.
Save bgerrissen/1058592 to your computer and use it in GitHub Desktop.
// some base class
var Dispatcher = function(){
this._listeners = [];
}
Dispatcher.prototype = {
listen: function(){},
deafen: function(){},
notify: function(){}
}
// setup some governing franchise
var xMagazine= new Dispatcher();
xMagazine.franchise = "xxx";
// actually use a helper method for this, but for clarity exposing underlying techique.
var blush = Object.clone(xMagazine);
typeof blush.constructor == "function" && blush.constructor.apply(blush);
var yMagazine = new Dispatcher();
yMagazine.franchise = "yyy";
var times = Object.clone(yMagazine);
typeof times.constructor == "function" && times.constructor.apply(times);
@bgerrissen
Copy link
Author

So instead of creating multiple prototypical constructors, I just use one constructor and then "sub-class" instances.
This means keeping the constructor clean and argumentless, which is a good convention in my opinion.
I still want each cloned instance to have their own _listeners Array, hence the use of the .constructor property.

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