码迷,mamicode.com
首页 > 其他好文 > 详细

jq2.1.4源码的相关方法的解读

时间:2015-12-08 15:56:35      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:

1:isArraylike(line:533)----js判断一个对象是不是类数组的对象;

   js中类数组的对象,你第一个想到的是什么,我的第一反应是function中的arguments对象,当然还有通过tagName获取到的元素集合;

   类数组的对象首先他得是一个对象(哈哈,废话一个),其次最关键的是有length属性;

function isArraylike( obj ) {

    // Support: iOS 8.2 (not reproducible in simulator)
    // `in` check used to prevent JIT error (gh-2145)
    // hasOwn isn‘t used here due to false negatives
    // regarding Nodelist length in IE
    var length = "length" in obj && obj.length,//判断对象是否存在length属性存在就赋值
        type = jQuery.type( obj );//获取对象下的类型
  //下方的if判断是做一个类型的甄别
    if ( type === "function" || jQuery.isWindow( obj ) ) {//对象类型不能是function 对象类型还不能符合jQuery.isWindow(obj)(稍后讨论)
        return false;
    }
  //如果obj是元素的话 并且存在length属性 那么必然他是一个 类数组的对象
    if ( obj.nodeType === 1 && length ) {
        return true;
    }
  /*  
  *下方的return 只要一个条件成立他就是类数组或者就是数组对象
  * type === "array";这个是通过 jQuery.type的扩展函数来获取的对象类型(稍后解读)
  * length === 0 这个是干啥的啊 让我想到了获取到了空的元素集合
  * typeof length === "number"&&length>0&&(length-1) in obj;判断这样的var json = {0:"as",1:"a",2:"b",3:"c",length:4};其实也是个类数组 他可以用for来获取除length以外的值
  */
return type === "array" || length === 0 || typeof length === "number" && length > 0 && ( length - 1 ) in obj; }

 2:jQuery.type( obj )判断对象的类型--(line:300)

    type: function( obj ) {
        if ( obj == null ) {
            return obj + "";
        }
        // Support: Android<4.0, iOS<6 (functionish RegExp)
     /*
     *以下判断是个三元运算
     *  条件:对象obj的类型是否为 object 或者 fuction
     *  条件成立时执行: class2type存在该对象的话返回class2type[object]或class2type[function],否则就返回"object"
     *  条件不成立时执行: 返回对象obj本身的类型(例如:number,string,boolean)
     */
return typeof obj === "object" || typeof obj === "function" ? class2type[ toString.call(obj) ] || "object" : typeof obj; },
class2type可以参考jq2.1.4源码的line:529;
class2type = {
[object Array]"array",[object Boolean]"boolean",[object Date]"date",[object Error]"error",[object Function]"function",[object Number]"number",[object Object]"object",[object RegExp]"regexp",[object String]"string"};

3:isWindow(obj)判断对象是否是window对象
isWindow: function( obj ) {
        return obj != null && obj === obj.window;
    },

此处我觉得最有意思的判断就在于他底下的obj === obj.window的判断;一般情概况下对象底下是不存在window属性的(除非手动定义了一个),但有一个例外;那就是window底下有window的属性并且两个指向的地址完全相同;

jq2.1.4源码的相关方法的解读

标签:

原文地址:http://www.cnblogs.com/tadini/p/5029248.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!