Created
October 26, 2012 14:30
-
-
Save mxriverlynn/3959151 to your computer and use it in GitHub Desktop.
An Object.create wrapper / shim for MarionetteJS
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
// Helpers | |
// ------- | |
// For slicing `arguments` in functions | |
var slice = Array.prototype.slice; | |
// Borrow the Backbone `extend` method so we can use it as needed | |
Marionette.extend = Backbone.Model.extend; | |
// A wrapper / shim for `Object.create`. Uses native `Object.create` | |
// if available, otherwise shims it in place for Marionette to use. | |
Marionette.createObject = (function(){ | |
var createObject; | |
// Define this once, and just replace the .prototype on it as needed, | |
// to improve performance in older / less optimized JS engines | |
function F() {} | |
// Check for existing native / shimmed Object.create | |
if (typeof Object.create === "function"){ | |
// found native/shim, so use it | |
createObject = Object.create; | |
} else { | |
// An implementation of the Boodman/Crockford delegation | |
// w/ Cornford optimization, as suggested by @unscriptable | |
// https://gist.github.com/3959151 | |
// native/shim not found, so shim it ourself | |
createObject = function (o) { | |
// set the prototype of the function | |
// so we will get `o` as the prototype | |
// of the new object instance | |
F.prototype = o; | |
// create a new object that inherits from | |
// the `o` parameter | |
var child = new F(); | |
// clean up just in case o is really large | |
F.prototype = null; | |
// send it back | |
return child; | |
}; | |
} | |
return createObject; | |
})(); |
If you'd like to attribute the original authors of your createObject function, you could add this:
// Boodman/Crockford delegation w/ Cornford optimization
Those guys are the ones that figured out that particular algorithm. :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
will definitely use these suggestions. thanks! :)