Home > Web Front-end > JS Tutorial > body text

The difference between js arrow functions and ordinary functions

藏色散人
Release: 2019-04-13 10:45:24
Original
4244 people have browsed it

This article mainly introduces to you the difference between javascript arrow functions and ordinary functions. I hope it will be helpful to friends in need!

Arrow functions - a new feature introduced in ES6 - support writing concise functions in JavaScript. Although normal functions and arrow functions work similarly, there are some interesting differences between them, as explained below.

Syntax

Syntax of ordinary functions:

let x = function function_name(parameters){ 
   // 函数体
};
Copy after login

Examples of ordinary functions:

let square = function(x){ 
  return (x*x); 
}; 
console.log(sqaure(9));
Copy after login

Output :

The difference between js arrow functions and ordinary functions

Syntax of arrow function:

let x = (parameters) => { 
    // 函数体
};
Copy after login

Example of arrow function:

var square = (x) => { 
    return (x*x); 
}; 
console.log(square(9));
Copy after login

Output :

The difference between js arrow functions and ordinary functions

Use the this keyword

Unlike ordinary functions, arrow functions do not have their own this.

For example:

let user = { 
    name: "GFG", 
    gfg1:() => { 
        console.log("hello " + this.name); 
    }, 
    gfg2(){        
        console.log("Welcome to " + this.name); 
    }   
 }; 
user.gfg1(); 
user.gfg2();
Copy after login

Output:

The difference between js arrow functions and ordinary functions

##arguments object availability

arguments objects are not available in arrow functions, but are available in normal functions.

Example of ordinary function:

let user = {       
    show(){ 
        console.log(arguments); 
    } 
}; 
user.show(1, 2, 3);
Copy after login

Output:


The difference between js arrow functions and ordinary functions

of arrow function Example:

let user = {      
        show_ar : () => { 
        console.log(...arguments); 
    } 
}; 
user.show_ar(1, 2, 3);
Copy after login

Output:


The difference between js arrow functions and ordinary functions

##Use the new keyword

Use the function Ordinary functions created by declarations or expressions are "constructible" and "callable". Since normal functions are constructible, they can be called using the 'new' keyword. However, arrow functions are only "callable" and not constructible. Therefore, we will get a runtime error while trying to construct a non-constructible arrow function using the new keyword.

Example of normal function:

let x = function(){ 
    console.log(arguments); 
}; 
new x =(1,2,3);
Copy after login
Output:

The difference between js arrow functions and ordinary functions

Example of arrow function:

let x = ()=> { 
    console.log(arguments); 
}; 
new x(1,2,3);
Copy after login
Output:


The difference between js arrow functions and ordinary functionsRelated recommendations: "

javascript tutorial

"

The above is the detailed content of The difference between js arrow functions and ordinary functions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template