Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The basic concept of JS function expression

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report