0002 - function closure

// add closure wrapped
_.mixin({
    closure: function() {
        var arg = arguments;
        return function() {
            arg[0].apply(arg[1], Array.prototype.slice.call(arg).slice(2));
        };
    }
});
借助了underscore的mixin,即扩展他的方法

// test and usage

var foo = function(name) {
    console.log(this, name);
};
foo('jarred');
var foo2 = _.closure(foo, {name: 'larry'}, 'larry');
foo2();




你可能感兴趣的:(0002 - function closure)