一段奇葩的代碼引發(fā)的問題

實際的代碼并不是這樣,我簡化了欲芹。

const hello = (list) => {
    list = list.map((item, index) => {
        if (index === 3) {
            list.splice(index, 1)
        }

        return item
    })

    console.log(list)
}
hello([1, 2, 3, 4]) // [1,2,3,4]

為什么返回的結(jié)果是[1,2,3,4]?而不是[1,2,3]吟吝。
這個時候需要map的源碼

// Production steps of ECMA-262, Edition 5, 15.4.4.19
// Reference: http://es5.github.io/#x15.4.4.19
if (!Array.prototype.map) {

  Array.prototype.map = function(callback/*, thisArg*/) {

    var T, A, k;

    if (this == null) {
      throw new TypeError('this is null or not defined');
    }

    // 1. Let O be the result of calling ToObject passing the |this| 
    //    value as the argument.
    var O = Object(this);

    // 2. Let lenValue be the result of calling the Get internal 
    //    method of O with the argument "length".
    // 3. Let len be ToUint32(lenValue).
    var len = O.length >>> 0;

    // 4. If IsCallable(callback) is false, throw a TypeError exception.
    // See: http://es5.github.com/#x9.11
    if (typeof callback !== 'function') {
      throw new TypeError(callback + ' is not a function');
    }

    // 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
    if (arguments.length > 1) {
      T = arguments[1];
    }

    // 6. Let A be a new array created as if by the expression new Array(len) 
    //    where Array is the standard built-in constructor with that name and 
    //    len is the value of len.
    A = new Array(len);

    // 7. Let k be 0
    k = 0;

    // 8. Repeat, while k < len
    while (k < len) {

      var kValue, mappedValue;

      // a. Let Pk be ToString(k).
      //   This is implicit for LHS operands of the in operator
      // b. Let kPresent be the result of calling the HasProperty internal 
      //    method of O with argument Pk.
      //   This step can be combined with c
      // c. If kPresent is true, then
      if (k in O) {

        // i. Let kValue be the result of calling the Get internal 
        //    method of O with argument Pk.
        kValue = O[k];

        // ii. Let mappedValue be the result of calling the Call internal 
        //     method of callback with T as the this value and argument 
        //     list containing kValue, k, and O.
        mappedValue = callback.call(T, kValue, k, O);

        // iii. Call the DefineOwnProperty internal method of A with arguments
        // Pk, Property Descriptor
        // { Value: mappedValue,
        //   Writable: true,
        //   Enumerable: true,
        //   Configurable: true },
        // and false.

        // In browsers that support Object.defineProperty, use the following:
        // Object.defineProperty(A, k, {
        //   value: mappedValue,
        //   writable: true,
        //   enumerable: true,
        //   configurable: true
        // });

        // For best browser support, use the following:
        A[k] = mappedValue;
      }
      // d. Increase k by 1.
      k++;
    }

    // 9. return A
    return A;
  };
}

看源碼就能知道菱父。在call之前,進入了while循環(huán)剑逃。即使改變了原有l(wèi)ist的數(shù)據(jù)結(jié)構(gòu)滞伟,但是返回了新的list
那我有一個小問題。如果代碼是這樣呢炕贵?

const hello = (list) => {
    list = list.map((item, index) => {
        if (index === 2) {
            list.splice(index, 1)
        }

        return item
    })

    console.log(list)
}
hello([1, 2, 3, 4]) 

返回的結(jié)果會是什么梆奈?

參考文檔:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map#Polyfill

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市称开,隨后出現(xiàn)的幾起案子亩钟,更是在濱河造成了極大的恐慌,老刑警劉巖鳖轰,帶你破解...
    沈念sama閱讀 222,104評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件清酥,死亡現(xiàn)場離奇詭異,居然都是意外死亡蕴侣,警方通過查閱死者的電腦和手機焰轻,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來昆雀,“玉大人辱志,你說我怎么就攤上這事∧欤” “怎么了揩懒?”我有些...
    開封第一講書人閱讀 168,697評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長挽封。 經(jīng)常有香客問我已球,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,836評論 1 298
  • 正文 為了忘掉前任智亮,我火速辦了婚禮忆某,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘阔蛉。我一直安慰自己弃舒,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,851評論 6 397
  • 文/花漫 我一把揭開白布馍忽。 她就那樣靜靜地躺著,像睡著了一般燕差。 火紅的嫁衣襯著肌膚如雪遭笋。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,441評論 1 310
  • 那天徒探,我揣著相機與錄音瓦呼,去河邊找鬼。 笑死测暗,一個胖子當(dāng)著我的面吹牛央串,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播碗啄,決...
    沈念sama閱讀 40,992評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼质和,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了稚字?” 一聲冷哼從身側(cè)響起饲宿,我...
    開封第一講書人閱讀 39,899評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎胆描,沒想到半個月后瘫想,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,457評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡昌讲,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,529評論 3 341
  • 正文 我和宋清朗相戀三年国夜,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片短绸。...
    茶點故事閱讀 40,664評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡车吹,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出醋闭,到底是詐尸還是另有隱情礼搁,我是刑警寧澤,帶...
    沈念sama閱讀 36,346評論 5 350
  • 正文 年R本政府宣布目尖,位于F島的核電站馒吴,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜饮戳,卻給世界環(huán)境...
    茶點故事閱讀 42,025評論 3 334
  • 文/蒙蒙 一豪治、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧扯罐,春花似錦负拟、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至秸歧,卻和暖如春厨姚,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背键菱。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評論 1 272
  • 我被黑心中介騙來泰國打工谬墙, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人经备。 一個月前我還...
    沈念sama閱讀 49,081評論 3 377
  • 正文 我出身青樓拭抬,卻偏偏與公主長得像,于是被迫代替她去往敵國和親侵蒙。 傳聞我的和親對象是個殘疾皇子造虎,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,675評論 2 359

推薦閱讀更多精彩內(nèi)容