箭頭函式 普通函式

2021-10-10 18:46:54 字數 965 閱讀 3881

箭頭函式的語法比普通函式更簡潔,是乙個匿名函式,不能作為建構函式,且不能使用new

//普通函式

letfun=(

)=>

//普通函式

function

fun(

)

箭頭函式有兩種格式:一種是和return ,另一種是把二者省略掉

1-1.不能使用new

let

fun=()

=>

let fn =

newfun()

;

1.

function

one(a)

one(1,

2,3,

4,5,

9);2.

lettwo=(

...b)

=>b(

12,32,

54,434)

;

var a=

200;

let obj=

fun:()

=>

}obj.fn(

);//100

obj.

fun();

//200

let obj=

c:function

(n)return f.

call

(m,n)}}

console.

log(obj.b(

1)) console.

log(obj.c(

1))

vara=

()=>

functionb(

)console.

log(a.prototype)

console.

log(b.prototype)

箭頭函式與普通函式區別

1 箭頭函式是匿名函式,不能作為建構函式,不能使用new 2 箭頭函式不繫結arguments,取而代之用rest引數 解決 3 this的作用域不同,箭頭函式不繫結this,會捕獲函式定義的上下文中的this值,作為自己的this值,且一直不變 4 箭頭函式沒有原型物件 5 箭頭函式不能當作gen...

箭頭函式和普通函式對比

let xx item item 單條語句可以省略return和 單個引數可以省略 let a let a new a 報錯 function a let a new a 不報錯arguments的一般使用場景是 允許傳入3個引數,中間乙個引數是可選。如果只傳1個參就是引數1用,傳入2個參就是引數1...

箭頭函式和普通函式的區別

面試時被問到箭頭函式和普通函式的區別。借用別人的乙個栗子 function make var testfunc make.call testfunc testfunc.call object object可以看到箭頭函式在定義之後,this 就不會發生改變了,無論用什麼樣的方式呼叫它,this 都不...