天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

JavaScript Array擴展實現代碼

indexOf
返回元素在數組的索引,沒有則返回-1。與string的indexOf方法差不多。
如果其他瀏覽器沒有實現此方法,可以用以下代碼實現兼容:
復制代碼 代碼如下:
Array.prototype.indexOf = function(el, start) {
    var start = start || 0;
    for ( var i=0; i < this.length; ++i ) {
        if ( this[i] === el ) {
            return i;
        }
    }
    return -1;
};
var array = [2, 5, 9];
var index = array.indexOf(2);
// index is 0
index = array.indexOf(7);
// index is -1

lastIndexOf
與string的lastIndexOf方法差不多。
如果其他瀏覽器沒有實現此方法,可以用以下代碼實現兼容:
復制代碼 代碼如下:
Array.prototype.indexOf = function(el, start) {
    var start = start || 0;
    for ( var i=0; i < this.length; ++i ) {
        if ( this[i] === el ) {
            return i;
        }
    }
    return -1;
};

forEach
各類庫中都實現相似的each方法。
如果其他瀏覽器沒有實現此方法,可以用以下代碼實現兼容:
復制代碼 代碼如下:
Array.prototype.forEach = function(fn, thisObj) {
    var scope = thisObj || window;
    for ( var i=0, j=this.length; i < j; ++i ) {
        fn.call(scope, this[i], i, this);
    }
};
function printElt(element, index, array) {
    print("[" + index + "] is " + element); // assumes print is already defined
}
[2, 5, 9].forEach(printElt);
// Prints:
// [0] is 2
// [1] is 5
// [2] is 9

every
如果數組中的每個元素都能通過給定的函數的測試,則返回true,反之false。換言之給定的函數也一定要返回true與false
如果其他瀏覽器沒有實現此方法,可以用以下代碼實現兼容:
復制代碼 代碼如下:
Array.prototype.every = function(fn, thisObj) {
    var scope = thisObj || window;
    for ( var i=0, j=this.length; i < j; ++i ) {
        if ( !fn.call(scope, this[i], i, this) ) {
            return false;
        }
    }
    return true;
};
function isBigEnough(element, index, array) {
  return (element <= 10);
}
var passed = [12, 5, 8, 130, 44].every(isBigEnough);
// passed is false
passed = [12, 54, 18, 130, 44].every(isBigEnough);
// passed is true

some
類似every函數,但只要有一個通過給定函數的測試就返回true。
如果其他瀏覽器沒有實現此方法,可以用以下代碼實現兼容:
復制代碼 代碼如下:
Array.prototype.some = function(fn, thisObj) {
    var scope = thisObj || window;
    for ( var i=0, j=this.length; i < j; ++i ) {
        if ( fn.call(scope, this[i], i, this) ) {
            return true;
        }
    }
    return false;
};
function isBigEnough(element, index, array) {
  return (element >= 10);
}
var passed = [2, 5, 8, 1, 4].some(isBigEnough);
// passed is false
passed = [12, 5, 8, 1, 4].some(isBigEnough);
// passed is true

filter
把符合條件的元素放到一個新數組中返回。
如果其他瀏覽器沒有實現此方法,可以用以下代碼實現兼容:
復制代碼 代碼如下:
Array.prototype.filter = function(fn, thisObj) {
    var scope = thisObj || window;
    var a = [];
    for ( var i=0, j=this.length; i < j; ++i ) {
        if ( !fn.call(scope, this[i], i, this) ) {
            continue;
        }
        a.push(this[i]);
    }
    return a;
};
function isBigEnough(element, index, array) {
  return (element <= 10);
}
var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);

map
讓數組中的每一個元素調用給定的函數,然后把得到的結果放到新數組中返回。。
如果其他瀏覽器沒有實現此方法,可以用以下代碼實現兼容:
復制代碼 代碼如下:
Array.prototype.map = function(fn, thisObj) {
    var scope = thisObj || window;
    var a = [];
    for ( var i=0, j=this.length; i < j; ++i ) {
        a.push(fn.call(scope, this[i], i, this));
    }
    return a;
};
<div id="highlighter_240589">
var numbers = [1, 4, 9];
var roots = numbers.map(Math.sqrt);
// roots is now [1, 2, 3]
// numbers is still [1, 4, 9]

reduce
讓數組元素依次調用給定函數,最后返回一個值,換言之給定函數一定要用返回值。
如果其他瀏覽器沒有實現此方法,可以用以下代碼實現兼容:
復制代碼 代碼如下:
Array.prototype.reduce = function(fun /*, initial*/)
{
  var len = this.length >>> 0;
  if (typeof fun != "function")
    throw new TypeError();
  if (len == 0 && arguments.length == 1)
    throw new TypeError();
  var i = 0;
  if (arguments.length >= 2){
    var rv = arguments[1];
  } else{
    do{
      if (i in this){
        rv = this[i++];
        break;
      }
      if (++i >= len)
        throw new TypeError();
    }while (true);
  }

  for (; i < len; i++){
    if (i in this)
      rv = fun.call(null, rv, this[i], i, this);
  }
  return rv;
};

復制代碼 代碼如下:
var total = [0, 1, 2, 3].reduce(function(a, b){ return a + b; });
// total == 6

JavaScript技術JavaScript Array擴展實現代碼,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 久久精品国产亚洲AV麻豆欧美玲 | 超碰视频97av| 国产亚洲精品97在线视频一 | 久久成人免费大片 | 国产精品高清在线观看93 | 久久久GOGO无码啪啪艺术 | 国产欧美另类久久久精品免费 | 高H短篇辣肉纯肉 | 天美传媒 免费观看 | 欧美精品v欧洲高清 | 欧美四虎精品二区免费 | 日韩一区二区天海翼 | 国偷自产AV一区二区三区健身房 | 中文字幕亚洲欧美日韩2o19 | 99精品欧美一区二区三区美图 | china18一19 第一次 | 成人国产三级在线播放 | 国产午夜不卡在线观看视频666 | 久久中文字幕亚洲 | 亚洲阿v天堂在线2017 | 国产成人精品午夜福麻豆报告 | 欧美18在线 | 成a人片亚洲日本久久 | 人妻中文字幕乱人伦在线 | 第四色男人天堂 | 无码AV免费精品一区二区三区 | 老色哥网站 | 伦理片97影视网 | 国产极品美女视频福利 | 69SEX久久精品国产麻豆 | 国产午夜精品自在自线之la | 国产精品内射久久久久欢欢 | 果冻传媒2021精品影视 | 国产精品无码麻豆放荡AV | 国产AV午夜精品一区二区入口 | 无人区免费一二三四乱码 | 国产亚洲视频中文字幕 | 国产精品人妻系列21P | 92午夜理论第1000集 app | 91免费网站在线看入口黄 | 精品国产麻豆免费人成网站 |