Skip to content

Instantly share code, notes, and snippets.

@mxriverlynn
Created October 26, 2012 14:30
Show Gist options
  • Save mxriverlynn/3959151 to your computer and use it in GitHub Desktop.
Save mxriverlynn/3959151 to your computer and use it in GitHub Desktop.
An Object.create wrapper / shim for MarionetteJS
// 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;
})();
@unscriptable
Copy link

cool. abstracting (rather than shimming) helps eliminate some potential compatibility issues. we still prefer shimming some things, like Object.create() since it's well established.

If you're interested, I see the potential for two minor modifications. One is for a slight perf boost. The other is just a tiny bit of cleanup which probably isn't needed.

// 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;

  if (typeof Object.create === "function"){

    // found native, use it
    createObject = Object.create;

  } else {

    // native not found, shim it
    createObject = function (o) {
      var child;
      F.prototype = o;
      child = new F();
      F.prototype = null; // clean up just in case o is really large
      return child;
    };

  }

  return createObject;

  // hoisted to closure scope so even poorly-optimized js engines can run fast:
  function F() {}

})();

What do you think? :)

@mxriverlynn
Copy link
Author

will definitely use these suggestions. thanks! :)

@unscriptable
Copy link

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