日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

ES6-6 - this指向、箭头函数基本形式、rest运算符

發(fā)布時(shí)間:2023/12/10 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ES6-6 - this指向、箭头函数基本形式、rest运算符 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一 chrome斷點(diǎn)調(diào)試

  • 觀察函數(shù)調(diào)用棧
// 25min var x = 1; function foo(x, y = function () { x = 2; console.log(2) }) {var x = 3;y();console.log(x) } foo() console.log(x) // 2 3 1




var x = 1; function foo(x, y = function () { x = 2; console.log(x) }) {x = 3;y();console.log(x) } foo() console.log(x) // 2 2 1

二 this指向

this指向總結(jié)
箭頭函數(shù)的this

  • 默認(rèn)綁定規(guī)則:指向window(嚴(yán)格模式指向undefined)
  • 隱式綁定:誰(shuí)調(diào)用指向誰(shuí)
  • 顯示綁定:call、apply、bind
  • new
  • 三 箭頭函數(shù)

    => 胖箭頭

    const f = a => a // 當(dāng)參數(shù)只有一個(gè)時(shí)可省略括號(hào)() // 當(dāng)函數(shù)體只有一句return時(shí) 可省略括號(hào){}和return const f = () => 5
    • 箭頭函數(shù)中不存在arguments,可以用rest運(yùn)算符…args(數(shù)組)

    四 rest/spread運(yùn)算符…/不定參數(shù)

    • 展開或收集
    • 可以將數(shù)組展開
    function foo(x, y, z){console.log(x, y, z) } foo(...[1, 2, 3]) // 1 2 3 foo.apply(null, [1, 2, 3]) // 1 2 3 foo.apply(undefined, [1, 2, 3]) // 1 2 3
    • 收集的功能
    const sum = (...args) => args // 返回的args是數(shù)組而不是類數(shù)組 const a = [2, 3, 4] const b = [1, ...a, 5] console.log(b) // 1 2 3 4 5 [1].concat(a, [5]) // 1 2 3 4 5

    concat參數(shù)

    • rest收集必須是最后一個(gè)參數(shù),否則報(bào)錯(cuò)
    const fn = (a, ...b) => console.log(b) fn(1, 2, 3, 4, 5) // [2,3,4,5]真正的數(shù)組

    ==argument數(shù)組排序 ==

    • 對(duì)函數(shù)形參的影響,…不計(jì)入length(默認(rèn)值也會(huì))
    const fn = (a, ...b) => a console.log(fn.length) // 1

    總結(jié)

    以上是生活随笔為你收集整理的ES6-6 - this指向、箭头函数基本形式、rest运算符的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。