mardi 20 octobre 2015

Trying to recreate the .isFunction() from the jQuery source

I'm trying to learn some thing from jQuery source by breaking some pieces apart. Right now I'm going over the isFunction. In my code test is instead of jQuery. The problem is that isFunction is telling me that stub is not a function by outputting false stub is a function.

I noticed that jquery doesn't use the this in return this.type(obj) === "function" like in my code and instead of writing the code in test.prototype ={, jQuery writes it in jQuery.extend({ maybe that's my problem? I didnt include extend function.

Try to keep the answer like jQuery's source code structure I know my code is a little different.

Also, Do you know of a smaller library like jQuery that I could use to learn from? I don't need to learn about AJAX and promises right now I just want to learn about structure of code and DOM manipulation.There's so much information in jQuery.

var test = function(){

}
var class2type = {};
test.prototype ={
    type : function(obj){
        if(obj == null){
            return obj + "";
        }
        return typeof obj === "object" || typeof obj === "function" ?
                    class2type[ toString.call(obj) ] || "object" :
                    typeof obj;
    },
    isFunction :function(obj){
        return this.type(obj) === "function"
    }
}


var stub = function stub(){}
console.log(typeof stub)
console.log(test.prototype.isFunction(stub)) 

Aucun commentaire:

Enregistrer un commentaire