Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mootools code explanation

I am trying to learn Mootools by reading the source and I don't understand why it makes a local copy of Function:

var Function = this.Function;

but why it doesn't make a local copy of Array, Number and String to do the same, for example they first appear being directly assigned to, so why treat Function differently?

Function.from = function(item){
    return (typeOf(item) == 'function') ? item : function(){
        return item;
    };
};

Array.from = function(item){
    if (item == null) return [];
    return (Type.isEnumerable(item) && typeof item != 'string') ? (typeOf(item) == 'array') ? item : slice.call(item) : [item];
};

Number.from = function(item){
    var number = parseFloat(item);
    return isFinite(number) ? number : null;
};

String.from = function(item){
    return item + '';
};

Also I don't understand how on line 149 Function calls the implement function which is stored on its local prototype property?

Function.implement({

hide: function(){
    this.$hidden = true;
    return this;
},

protect: function(){
    this.$protected = true;
    return this;
}

});

is it because Function is a function so its internal [[prototype]] is Function.prototype ?

like image 919
Daniel Robinson Avatar asked Jan 23 '26 14:01

Daniel Robinson


1 Answers

  • You'll notice that Function is referenced more than the other constructors, so it could be that they added the local reference for a tiny performance boost, as well as for compression since the local variable can be obfuscated. ...(Taking another look, I do see more references to the other constructors than I originally had.)

  • You'll also notice that the .overloadSetter() chained onto the Function.prototype.extend function wraps that function with a bunch of extra code, duck typing and such. So the reason they didn't use it was probably that this extra code was apparently unwanted/unneeded for their internal use.

  • Because implement extends the prototype, it isn't always desirable. Sometimes you just don't want extra methods available on all instances, but you do want to store them in a logical namespace, much like the native methods stored on the Object constructor.