js 零零散散的總結。

來源:互聯網
上載者:User

標籤:

Array.slice.call(arguments);可以將一個類數組轉化為數組。
    (function(){        console.log(arguments);  //[] 是一個類數組        console.log(arguments instanceof Array);//false        console.log(typeof arguments);// object    var _arguments = Array.prototype.slice.call(arguments);        console.log(_arguments); // [] 數組        console.log(_arguments instanceof Array);//true        console.log(typeof _arguments); //object})();
array.reduce(callback[, initialValue]);實現二維數組扁平化。
var flatten = matrix.reduce(function (previous, current) {  return previous.concat(current);});console.log(flatten); //[1,2,3,4,5,6]
array.reduce(callback[, initialValue])

callback函數接受4個參數:之前值、當前值、索引值以及數組本身。initialValue參數可選,表示初始值。若指定,則當作最初使用的previous值;如果預設,則使用數組的第一個元素作為previous初始值,同時current往後排一位,相比有initialValue值少一次迭代。

var sum = [1, 2, 3, 4].reduce(function (previous, current, index, array) {  return previous + current;});console.log(sum); // 10

相容性封裝(針對ie6-8)

if (typeof Array.prototype.reduce != "function") {  Array.prototype.reduce = function (callback, initialValue ) {     var previous = initialValue, k = 0, length = this.length;     if (typeof initialValue === "undefined") {        previous = this[0];        k = 1;     }         if (typeof callback === "function") {      for (k; k < length; k++) {         this.hasOwnProperty(k) && (previous = callback(previous, this[k], k, this));      }    }    return previous;  };}

 

js 零零散散的總結。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.