Difference between Regular Function vs Arrow Function

Tram Ho

I’m TUAN, currently a Full-stack Developer in Tokyo.
If you find this Blog interesting, please give me a like and subscribe to support me.

You can declare your Functions in many ways.

Use keyword function:

You can call both declaration and expression functions as Normal/Regular Function

Arrow function  was introduced in ES6 and is also known as arrow function.

As you can see both function works the same in the above example. Now the question is why do we need regular funcion or arrow function.

Let’s discuss some issues below

1. Syntax – Syntax

2. Arguments binding – Argument binding

3. this

4. keyword: new

5. No duplicate named parameters

6. Function Hoisting

7. Method

———————————————–

1. Syntax – Syntax

We can write regular and arrow function this way

Implicit Return – Default Return

In a regular function, you must use the return keyword to return any value . If you don’t return anything, the function returns undefined.

The arrow functions work the same way when returning a value.

If arrow function contains an expression, you can omit the curly braces and then the expression will be returned implicitly returned.

{}optional if it has only one line statement

()optional if you have only one argument

let add = (x) => x + x;

If no argument

let arrowFunc = _ => console.log("Arrow Function");

2. Arguments binding – Binding Arguments

In a regular function, the arguments keywords can be used to access arguments passed to the function.

Example:

The arrow functions have no argument binding.

However, if you want to access the arguments in a arrow function, you can use the rest:

operator

3. this

In a regular function, this changes according to how the function is called.

  • Simple Invocation: this with a global object or may be undefined if you are using mode strict mode.
  • Method Invocation: this with the object that owns the function.
  • Indirect Invocation: this with the first argument.
  • Constructor Invocation: this with the newly created instance.

The arrow functions don’t have their own this and they don’t redefine the value of this  inside the function .

this inside a arrow function always refers to this from contexts inside outside.

4. new

Common functions can use constructor, they can be called using the keyword new.

However, arrow functions can never be used as constructors. Therefore, they can never be called with the new

. keyword

5. paramaters cannot have duplicate names

In normal function we can do this:

arrow functions must never have duplicate named parameters, whether in strict mode or non-strict mode.

6. Function Hoisting

In normal function, function gets hoisting at the top.

In arrow function, the function is hoisted where you declare it. So if you call the function before declaration you will get referenceError.

7. Methods

You can define functions in a class using regular functions.

You also need to apply callback.

But if you bind this

By the example above, you can see that you must associate contexts this with their context.

In arrow function, you do not need to bind to context.

Khi nào không sử dụng arrow function

Object

Khi bạn gọi dog.jumps, số đếm không tăng lên. Đó là bởi vì this không bị ràng buộc với bất cứ điều gì, và sẽ kế thừa value của this từ phạm vi cha của nó.

Tham khảo

Tóm tắt

Trong hàm thông thường, this value là động, Trong arrow function, this value bằng với value của hàm ngoài.

Trong hàm thông thường, các đối số sẽ cung cấp cho bạn danh sách tham số arguments được truyền vào hàm. argument của arrow function không được xác định.

Trong hàm thông thường, bạn luôn phải trả về bất kỳ value nào, nhưng trong arrow function, bạn có thể bỏ qua keyword return và có thể viết single line.

Trong mũi tên, các tham số của hàm phải là duy nhất.

Vấn đề Hoisting trong arrow function vì hàm không được gọi trước khi khởi tạo.

Như mọi khi, mình hy vọng bạn thích bài viết này và biết thêm được điều gì đó mới.

Cảm ơn và hẹn gặp lại các bạn trong những bài viết tiếp theo!

Nếu bạn thấy thích blog của mình thì nhấn theo dõi để ủng hộ mình nhé. Thank you.

Ref

Share the news now

Source : Viblo