ES6 函式的拓展 四

2022-08-24 11:45:06 字數 1026 閱讀 7257

一、引數帶預設值函式

1、在函式形參可以賦予函式預設值【即實參嚴格匹配undefined時,在函式內部使用形參時呼叫它的預設值】

2、函式name屬性 【返回函式名稱,無名的函式返回空字串】

3、函式length屬性  【從左往右開始計算函式形參直到碰到帶有預設值形參時結束計算,這樣計算的形參個數】

eg:

//

普通函式傳參

function test(a,b=2,c,d=3)

test(3,4,5); //

輸出abcd分別為:3,4,5,3

console.log(test.name); //

test

console.log(test.length) //1//

使用解構傳遞引數,以物件解構為例

function add() =

add(); //

使用物件解構 輸出a b為:2 5

4、rest引數

在函式形參那是有擴充套件運算子接受實參【返

回接受的引數組成的陣列】

eg:function test(a,b,...tail)

test(1,2,3,4);

//引數陣列解構傳遞

function add([a,...tail])

add([1,2,3,4]);

二、箭頭函式

1、es6中箭頭函式中的this指向為該箭頭函式【宣告】時所處的父模組作用域中的this指向

2、es6中箭頭函式中的arguments不儲存實參,不進行實參的使用

3、不能作為建構函式【其函式內this指向可能不會指向該類例項物件】

eg:

//

箭頭函式內部沒有this,this指向箭頭函式【宣告時】的父作用域中的this

function

test();

}let obj =;

obj.test();

//這裡執行的是test()函式中返回的箭頭函式而不是外面定義的test()函式

es6的函式拓展

參考 1.增加函式預設值 es6允許為函式提供預設值,與解構賦值一起使用,非常地方便 function foo foo 相當於執行let 輸出undefined 5 foo 相當於執行let 輸出 1 5 foo 1 2 foo typeerror cannot read property x of...

ES6 函式拓展內容

es6允許為函式的引數設定預設值,即直接寫在引數定義的後面。function log x,y world log hello hello world log hello yivi hello yivi log hello hello引數變數是預設宣告的,因此不能再用let或const再次宣告,也不能...

es6 陣列拓展

陣列 屬性 constructor,length,prototype 方法 push,pop,shift,unshift 新增 splice,slice,reverse,sort,concat,filter,map,some,every 1 filter 過濾出符合條件的值,返回新陣列 var sp...