Detailed explanation of the difference between arrow functions and normal functions in JavaScript

Detailed explanation of the difference between arrow functions and normal functions in JavaScript

This article explains the difference between arrow functions and ordinary functions in JavaScript for your reference. The specific content is as follows

Arrow functions:

let fun = () => {
    console.log('lalalala');
}

Ordinary functions:

function fun() {
    console.log('lalla');
}

Arrow functions are equivalent to anonymous functions and simplify function definitions. Arrow functions have two formats. One contains only one expression, with { ... } and return omitted. There is also a type that can contain multiple statements, in which case { ... } and return cannot be omitted.

Arrow functions are anonymous functions and cannot be used as constructors or with new.

let FunConstructor = () => {
    console.log('lll');
}

let fc = new FunConstructor();

Arrow functions do not bind arguments, but use rest parameters instead...Solution

function A(a){
  console.log(arguments);
}
A(1,2,3,4,5,8); // [1, 2, 3, 4, 5, 8, callee: ƒ, Symbol(Symbol.iterator): ƒ]


let B = (b)=>{
  console.log(arguments);
}
B(2,92,32,32); // Uncaught ReferenceError: arguments is not defined


let C = (...c) => {
  console.log(c);
}
C(3,82,32,11323); // [3, 82, 32, 11323]

Arrow functions do not bind this, but capture the this value of the context in which they are located as their own this value

var obj = {
  a: 10,
  b: () => {
    console.log(this.a); // undefined
    console.log(this); // Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, frames: Window, …}
  },
  c: function() {
    console.log(this.a); // 10
    console.log(this); // {a: 10, b: ƒ, c: ƒ}
  }
}
obj.b(); 
obj.c();
var obj = {
  a: 10,
  b: function(){
    console.log(this.a); //10
  },
  c: function() {
     return ()=>{
           console.log(this.a); //10
     }
  }
}
obj.b(); 
obj.c()();

When an arrow function calls a function through the call() or apply() method, only one parameter is passed in and it has no effect on this .

let obj2 = {
    a: 10,
    b: function(n) {
        let f = (n) => n + this.a;
        return f(n);
    },
    c: function(n) {
        let f = (n) => n + this.a;
        let m = {
            a: 20
        };
        return f.call(m,n);
    }
};
console.log(obj2.b(1)); // 11
console.log(obj2.c(1)); // 11

Arrow functions have no prototype property

var a = ()=>{
  return 1;
}

function b(){
  return 2;
}

console.log(a.prototype); // undefined
console.log(b.prototype); // {constructor: ƒ}

Arrow functions cannot be used as Generator functions and cannot use the yield keyword

Summarize

  • The this of an arrow function always points to the this of its context, and no method can change its reference, such as call() , bind() , apply()
  • The this of a normal function refers to the object that calls it.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Tips for writing better JavaScript conditionals and matching conditions (summary)
  • Detailed explanation of this pointing in JS arrow function
  • Characteristics of JavaScript arrow functions and differences from ordinary functions
  • What scenarios are not suitable for JS arrow functions?
  • Which scenarios in JavaScript cannot use arrow functions
  • Introduction to JavaScript conditional access attributes and arrow functions

<<:  Nexus uses nginx proxy to support HTTPS protocol

>>:  Summary of Mysql high performance optimization skills

Recommend

Advantages of INSERT INTO SET in MySQL

Insert data into mysql database. Previously commo...

Example sharing of anchor tag usage in HTML

Anchor tag usage: Linking to a specific location i...

Solution for installing opencv 3.2.0 in Ubuntu 18.04

Download opencv.zip Install the dependencies ahea...

Example of how to deploy MySQL 8.0 using Docker

1. Refer to the official website to install docke...

Some useful meta setting methods (must read)

<meta name="viewport" content="...

Solution to Ubuntu 20.04 Firefox cannot play videos (missing flash plug-in)

1. Flash plug-in package download address: https:...

nginx+tomcat example of accessing the project through the domain name

I was curious about how to access the project usi...

MySQL database development specifications [recommended]

Recently, we have been capturing SQL online for o...

Div exceeds hidden text and hides the CSS code beyond the div part

Before hiding: After hiding: CSS: Copy code The co...

Detailed explanation of Javascript basics

Table of contents variable Data Types Extension P...

Detailed tutorial on compiling and installing MySQL 5.7.24 on CentOS7

Table of contents Install Dependencies Install bo...

Introduction to Docker Architecture

Docker includes three basic concepts: Image: A Do...

Web page experience: Web page color matching

<br />The color of a web page is one of the ...