Created
August 26, 2015 09:27
-
-
Save ironboy/85c2f2d8a9153974bc4e to your computer and use it in GitHub Desktop.
Minimal prototypical inheritance patterns
This file contains 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
// 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