javascript - How to interpret the third parameter args of jQuery each method?
大家讲道理
大家讲道理 2017-07-05 10:53:30
0
1
1130

When I was free, I looked at the jq source code and found that the each method, map method, and makeArray all stated that they are limited to internal use (/ / arg is for internal usage only), I checked a lot of information but couldn't find the answer. Even jquery's API only gives an explanation of the first two parameters. Is it for debugging? How to use it?

source code

// args is for internal usage only
    each: function( obj, callback, args ) {
        var value,
            i = 0,
            length = obj.length,
            isArray = isArraylike( obj );

        if ( args ) {
            if ( isArray ) {
                for ( ; i < length; i++ ) {
                    value = callback.apply( obj[ i ], args );

                    if ( value === false ) {
                        break;
                    }
                }
            } else {
                for ( i in obj ) {
                    value = callback.apply( obj[ i ], args );

                    if ( value === false ) {
                        break;
                    }
                }
            }

        // A special, fast, case for the most common use of each
        } else {
            if ( isArray ) {
                for ( ; i < length; i++ ) {
                    value = callback.call( obj[ i ], i, obj[ i ] );

                    if ( value === false ) {
                        break;
                    }
                }
            } else {
                for ( i in obj ) {
                    value = callback.call( obj[ i ], i, obj[ i ] );

                    if ( value === false ) {
                        break;
                    }
                }
            }
        }

        return obj;
    }

After a lot of testing, I found that there are restrictions on the third parameter. The types that cannot be passed are: numbers, strings, json, etc.; can only pass arrays; I have not included other types. Once tested
When passing in the array, the callback must use as many parameters as the length of the args array to receive the values ​​​​in the args array one by one. If there are three values ​​​​in the array, the callback has only one formal parameter, then this Formal parameters can only receive the value of args[0]. And we can use arguments to print all the passed values ​​in args (all passed in order), one of the test codes:

// 首先each我传入了三个参数
// 通过这种方式,我取到了a,b,c 的三个值分别为'wo', 'ai', 'ni'
// 多次尝试,我发现args传过来的三个参数是按照顺序排列好的,
// 如果只有一个参数那么,我们就只能取到'wo',
// 当
var arr = [5, 7, 5];
$.each(
    arr,
    function(a, b, c){
        console.log(a); // 'wo'
        console.log(b); // 'ai'
        console.log(c); // 'ni'
        console.log(arguments); //["wo", "ai", "ni", callee: function, Symbol(Symbol.iterator): function]
        console.log(this) // 回调中的所有参数会根据each第一个参数的长度进行多次打印,所以每次打印的this都指向每一次的arr的值
    },
    ['wo', 'ai', 'ni']
)

refer to

I found that someone had asked this question before segmentfault, but unfortunately no one gave the answer
/q/10...

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(1)
巴扎黑

Thank you everyone, the problem is solved, I know why

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template