jQuery原理整理笔记2


1,静态方法each
    真数组
    var arr = [1,3,5,7,9];
    伪数组
    var obj = [0:1,1:3,2:5,3:7,4:9,length:5];

    原生的JS只能遍历数组
        第一个参数:当前遍历到的元素
        第二个参数:当前遍历到的索引
    arr.forEach(function (value,index){
        console.log(index,value);
    });

    jQuery:两个都可以遍历
        第一个参数:当前遍历到的索引
        第二个参数:当前遍历到的元素
    $.each(arr,function(index,value){
        console.log(index,value);
    });
    
    $.each(obj,function(index,value){
        console.log(index,value);
    });

2,静态方法map
    真数组
    var arr = [1,3,5,7,9];
    伪数组
    var obj = [0:1,1:3,2:5,3:7,4:9,length:5];

    原生的JS只能遍历数组
        第一个参数:当前遍历到的元素
        第二个参数:当前遍历到的索引
        第三个参数:当前遍历到的数组
    arr.map(function (value,index,array){
        console.log(index,value,array);
    });

    jQuery:
        第一个参数:当前遍历到的数组
        回调函数的参数:
        第一个参数:当前遍历到的元素
        第二个参数:当前遍历到的索引
    $.map(array,function(value,index){
        console.log(index,value);
    });

    $.map(obj,function(value,index){
        console.log(index,value);
    });

3,each和map的区别
    var res = $.each(obj,function(index,value){
        console.log(index,value);
    });

    var res2 = $.map(obj,function(value,index){
        console.log(index,value);
        return value + index;
    });
    each返回值:便利谁返回谁
    map返回值:空数组
    each不支持在回调函数中对遍历的数组进行处理
    map可以在回调函数中通过return对遍历数组进行处理,然后生成一个新的数组

4,其它静态方法
    $.trim();去字符串两端空格
    var str = "   hello   ";
    var res = $.trim(str);
    console.log("---"+str+"---");
    console.log("---"+res +"---");

    $.isWindow();判断传入的对象是否为window对象true/false
    真数组
    var arr = [1,3,5,7,9];
    伪数组
    var arrlike = [0:1,1:3,2:5,3:7,4:9,length:5];
    对象
    var obj = ["name":"hello",age:"20"];
    函数
    var fn= function(){};
    window对象
    var w = window;

    var res = $.isWindow(window);
    console.log(res);

    $.isArray();判断是否为真数组true/false
    
    $.isfunction();判断是否为函数true/false(jQuery本质就是一个函数)

5,静态方法holdReady方法(暂停ready执行)
    jQuery
        $.holdReady(true);
        $(document).ready(function(){
            alert("ready");
        });

猜你喜欢

转载自blog.csdn.net/LittleMangoYX/article/details/81591895