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

Ideas and codes for realizing magnifying glass effect in js

This article example shares the specific code of ...

How to solve the problem that Docker container has no vim command

Find the problem Today, when I tried to modify th...

Ant Design Blazor component library's routing reuse multi-tab function

Recently, there has been a growing demand for imp...

How to clear the validation prompt in element form validation

Table of contents Problem scenario: Solution: 1. ...

Make a nice flip login and registration interface based on html+css

Make a nice flip login and registration interface...

How to Monitor Linux Memory Usage Using Bash Script

Preface There are many open source monitoring too...

80 lines of code to write a Webpack plugin and publish it to npm

1. Introduction I have been studying the principl...

Summary of how JS operates on pages inside and outside Iframe

Table of contents Get the content of the iframe o...

Play with the connect function with timeout in Linux

In the previous article, we played with timeouts ...

Vue uses drag and drop to create a structure tree

This article example shares the specific code of ...

How to modify the master-slave replication options in MySQL online

Preface: The most commonly used architecture of M...

Pure CSS3 mind map style example

Mind Map He probably looks like this: Most of the...

Can Docker become the next "Linux"?

The Linux operating system has revolutionized the...