zoukankan      html  css  js  c++  java
  • JavaScript中forEach的用法相关

    首先说下JavaScript的forEach的标准格式。

    为数组中的每个元素执行指定操作。

    array1.forEach(callbackfn[, thisArg])
    

    参数

    定义

    array1

    必需。 一个数组对象。

    callbackfn

    必需。 一个接受最多三个参数的函数。 对于数组中的每个元素,forEach 都会调用 callbackfn 函数一次。

    thisArg

    可选。 可在 callbackfn 函数中为其引用 this 关键字的对象。 如果省略 thisArg,则 undefined 将用作 this 值。

    如果 callbackfn 参数不是函数对象,则将引发 TypeError 异常。

    对于数组中的每个元素,forEach 方法都会调用 callbackfn 函数一次(采用升序索引顺序)。 不为数组中缺少的元素调用该回调函数。

    除了数组对象之外,forEach 方法可由具有 length 属性且具有已按数字编制索引的属性名的任何对象使用。

    回调函数语法

    回调函数的语法如下所示:

    function callbackfn(value, index, array1)

    可使用最多三个参数来声明回调函数。

    回调函数的参数如下所示。

    回调参数

    定义

    value

    数组元素的值。

    index

    数组元素的数字索引。

    array1

    包含该元素的数组对象。

    修改数组对象

    forEach 方法不直接修改原始数组,但回调函数可能会修改它。

     一般方法的格式是:

    arrayx.forEach(function(value,index,arrayy){…})

    但对于NodeList要用下面的写法。

     [].forEach.call(lists,function(valule.index.arrayy){…})

     

    出处:http://technet.microsoft.com/zh-cn/ff679980%28v=vs.85%29

  • 相关阅读:
    LN : leetcode 283 Move Zeroes
    LN : Eden Polymorphic And OOP Design Pattern Abstract Factory
    LN : leetcode 242 Valid Anagram
    LN : leetcode 231 Power of Two
    LN : leetcode 191 Number of 1 Bits
    LN : leetcode 263 Ugly Number
    LN : leetcode 258 Add Digits
    LN : leetcode 292 Nim Game
    day5——Python整型、浮点型、布尔型及字符串
    day4——Python运算符
  • 原文地址:https://www.cnblogs.com/MY0101/p/5431593.html
Copyright © 2011-2022 走看看