jQuery源码分析

来源:互联网 发布:磨刀器哪个牌子好 知乎 编辑:程序博客网 时间:2024/05/22 01:36

文章只讨论内部的方法,至于结构什么的并不在讨论范围以内

function DOMEval( code, doc ) {        doc = doc || document;        var script = doc.createElement( "script" );        script.text = code;        doc.head.appendChild( script ).parentNode.removeChild( script );}

如上方法的作用是在html文档中增加一个script的节点并立即执行该script节点中所写的脚本,然后再从html文档中删除该script节点,使之与原文档一致。这个属于jQuery内部方法。

var arr = [];var document = window.document;//文档var getProto = Object.getPrototypeOf;//得到某个对象的原型var slice = arr.slice;//数组的裁剪方法var concat = arr.concat;//数组的连接方法var push = arr.push;//数组的插入方法var indexOf = arr.indexOf;//返回某个值在数组中的第一个匹配项的索引var class2type = {};var toString = class2type.toString;var hasOwn = class2type.hasOwnProperty;//确定某个对象是否具有带指定名称的属性(必须是自己的,而不是在原型中)var fnToString = hasOwn.toString;//var ObjectFunctionString = fnToString.call( Object );//"function Object() { [native code] }"var support = {};

以上是jQuery内置的一些参数

toArray: function() {        return slice.call( this );}

将对象转化为数组,this所表示的对象必须是可枚举的,实现了Iterator接口的

//$("div").get(1)  返回指定位置的dom元素    get: function( num ) {        // Return all the elements in a clean array        //可以直接返回 this         if ( num == null ) {            return slice.call( this );        }        // Return just the one element from the set        return num < 0 ? this[ num + this.length ] : this[ num ];    }
isWindow: function( obj ) {        return obj != null && obj === obj.window;    }

上方法判断是否是window对象

isPlainObject: function( obj ) {        var proto, Ctor;        // Detect obvious negatives        // Use toString instead of jQuery.type to catch host objects        if ( !obj || toString.call( obj ) !== "[object Object]" ) {            return false;        }        proto = getProto( obj );//Object.getPrototypeOf(a) 得到a的原型对象        // Objects with no prototype (e.g., `Object.create( null )`) are plain        //通过 var n = Object.create(null) 创造出来的对象,其原型是 Object.getPrototypeOf(n)            //=== null n.__proto__===undefined        //Object.getPrototypeOf(null) || Object.getPrototypeOf(undefined) 会抛错        if ( !proto ) {            return true;        }        // Objects with prototype are plain iff they were constructed by a global Object function        //c语言中&&是一种双目运算符,表示与运算,而当左边所给表达式或变量为0时,不再计算右侧,整个表达式为零。        //在js中当 第一个返回是 0 "" null undefined false 时候 返回第一个 否则返回第二个        Ctor = hasOwn.call( proto, "constructor" ) && proto.constructor;        return typeof Ctor === "function" && fnToString.call( Ctor ) === ObjectFunctionString;    }

以上方法判断该对象是否是纯的对象

isEmptyObject: function( obj ) {        /* eslint-disable no-unused-vars */        // See https://github.com/eslint/eslint/issues/6125        var name;        for ( name in obj ) {            return false;        }        return true;    }

以上方法判断是否为空对象

type: function( obj ) {        if ( obj == null ) {            return obj + "";        }// null+"" == "null"        // Support: Android <=2.3 only (functionish RegExp)        return typeof obj === "object" || typeof obj === "function" ?            class2type[ toString.call( obj ) ] || "object" :            typeof obj;    }

以上方法判断对象的类型

原创粉丝点击