Skip to content

Instantly share code, notes, and snippets.

@ryanflorence
Created May 19, 2011 13:57
Show Gist options
  • Save ryanflorence/980804 to your computer and use it in GitHub Desktop.
Save ryanflorence/980804 to your computer and use it in GitHub Desktop.
A more diplomatic MooTools Array
// A more diplomatic MooTools Array
// If in node or something
var MArray = require('mootools-array');
// API to define methods
MArray.define('each', function (fn, bind){
// use this, just like we do now
for (var i = 0, l = this.length; i < l; i++){
if (i in this) fn.call(bind, this[i], i, this);
}
});
var handler = function (number){ console.log(number) };
// Generic
MArray.forEach([1,2,3], handler);
// augment native Array
MArray.augment();
// Use it like we do now
[1,2,3].forEach(handler);
// or maybe do it opposite, by default, augment natives
// and then add a safeMode that undoes what it did
MArray.safeMode();
@ibolmo
Copy link

ibolmo commented May 19, 2011

The define method is the important part. It's married with the problem of AMD. Perhaps something like:

moo.define('Type', ['Core'], function(global){

});

moo.define('Array', ['Type'], function(global){

  global.Array.implement({

  });

});

moo.augment(); // would pass window (or GLOBAL) to the function
moo.safe(); // passes a clean similar to fusejs.

Food for thought. moo.define, i know looks familiar to requirejs (correct me).

@arian
Copy link

arian commented May 19, 2011

Would be nice if we can subclass the natives, and with the augment method implement them in the current natives (Array, Function, ...).

I know it's already possible with FuseBox by jdalton. I think there was also something like that in ES Harmony. Actual subclassing of Natives like Array, can be a bit of a pain: http://perfectionkills.com/how-ecmascript-5-still-does-not-allow-to-subclass-an-array/

@ryanflorence
Copy link
Author

There's some argument over mutable proto at mozilla, which is the key to FuseBox.

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