Skip to content

Instantly share code, notes, and snippets.

@ironboy
Created August 26, 2015 09:27
Show Gist options
  • Save ironboy/85c2f2d8a9153974bc4e to your computer and use it in GitHub Desktop.
Save ironboy/85c2f2d8a9153974bc4e to your computer and use it in GitHub Desktop.
Minimal prototypical inheritance patterns
// A minimal polyfill for Object.assign
// (an ES6 feature)
Object.assign = Object.assign || function(){
for(var i = 1; i < arguments.length; i++){
for(var j in arguments[i]){
arguments[0][j] = arguments[i][j];
}
}
};
// JavaScript has prototypical inheritance
// so there is no real difference in extending
// something or creating a new instance
// (although the new keyword may have tricked you
// into thinking so)
// A small Base object with a simple extend method
var Base = {
extend: function(props){
// A new object with this object as its prototype
var obj = Object.create(this);
// Assign properties to the new object
Object.assign(obj,props);
return obj;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment