Get the App
SLTechnology News&Howtos  ›  Development  › 

The basic concept of JS function expression

Shulou Source: shulou.com Published: 2022-06-03 11:53:12 10月18日 Update

This article mainly explains "the basic concept of JS function expression". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn the basic concepts of JS function expressions.

The basic concept of function expression name attribute and function promotion

First, the name property, through which you can access the name assigned to the function. (non-standard attribute) such as:

Function People () {}; console.log (People.name); / / People

Second, the function declaration is promoted, which means that the function declaration can be placed after the statement that calls it. Such as:

SayHi (); / / call the function function sayHi () {/ / declare the function console.log ("Hi");} / / will not report an error

You cannot use a function expression:

SayHi (); var sayHi = function () {console.log ("Hi");} / / error report

There are two ways to create a function, one is a function declaration (such as the first), and the other is a function expression (such as the second). The second function creation method creates a function called "anonymous function" or "Ramda function" because there is no identifier after the function keyword.

Common mistakes in function promotion

It is important to note that, for comparison, the first of the following two types of code is wrong (which leads to different problems for each browser); the second makes the correct one. The code is as follows:

Var condition = true;if (condition) {function sayHI () {console.log ("hi")} / / Welcome to join the front-end full stack development communication circle: 1007317281 sayHI (); / / "hello"} else {function sayHI () {console.log ("hello")} sayHI ();}

Error

Var condition = false;var sayHi;if (condition) {sayHi = function () {console.log ("hi")}; / / Welcome to join the front-end full stack development communication circle: 1007317281 sayHi ();} else {sayHi = function () {console.log ("hello")}; sayHi (); / / hello}

No mistakes.

Var condition = true;if (condition) {var sayHi = function () {console.log ("hi")}; sayHi (); / / hi} else {var sayHi = function () {console.log ("hello")}; sayHi (); / / hello}

There won't be a problem here. The root cause of the above problem is function promotion, which is caused by the difference between function declaration and function expression.

At this point, I believe you have a deeper understanding of "the basic concept of JS function expression". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Functions expressions concepts attributes ways communication learning errors problems code content front end learning communication that is welcome to join develop different practical deeper between Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Redmi Docker Xiaomi vpn