zoukankan      html  css  js  c++  java
  • 数组的foreach方法和jQuery中的each方法

    /*
    * 数组的forEach方法:
    * 1、返回给回调的参数先是值,然后是下标
    * 2、回调函数执行时内部的this指向window
    * */
    /*var arr = [1,2,3,4,5];
    arr.forEach(function( val, index ) {
    console.log( val, index, this );
    });*/


    /*
    * jQ实例的each方法,
    * 1、返回给回调的参数先是下标,然后是值
    * 2、回调函数执行时内部的this就指向遍历到的每一个值(就是回调中接收到的val)
    * 3、如果想中断遍历,在回调中返回false即可
    * */
    /*$('li').each( function( index, val ) {

    console.log( index, val, this );

    if( index === 1 ) {
    return false;
    }
    });*/

    /*
    * jQ还提供了一个静态版本的each方法,供框架使用者使用
    * 1、返回给回调的参数先是下标,然后是值
    * 2、回调函数执行时内部的this就指向遍历到的每一个值(就是回调中接收到的val)
    * 3、如果想中断遍历,在回调中返回false即可
    * */
    var obj = { name: 'test', val: {} };
    var arr2 = [ 'abc', {}, 'qwer' ];

    $.each( obj, function( key, val ) {
    console.log( key, val, this );
    } );

    $.each( arr2, function( index, val ) {
    console.log( index, val, this );
    } );

  • 相关阅读:
    World Cup
    Eva's Problem
    Number-guessing Game
    WisKey的眼神
    Vowel Counting
    The 3n + 1 problem
    超级楼梯
    母牛的故事
    素数回文
    画8
  • 原文地址:https://www.cnblogs.com/luxiaoxiao/p/6098089.html
Copyright © 2011-2022 走看看