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

What are the functions in JavaScript that you don't know?

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is to share with you about the functions in JavaScript that you don't know. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Function declaration

In JavaScript, there are two forms of expression for function declaration, declaration and expression, while other functions (such as immediate execution function, arrow function, etc.) are derived from these two forms.

/ / declarative function foo () {console.log ('I am declarative')} / / expression var foo = function () {console.log ('I am an expression')}

Named function

Therefore, the name Siyi, a named function is a named function, this is one of our most common functions, let's first take a look at how it is written.

/ / named function function foo () {console.log ('I am a named function')}

As mentioned above, there are two forms of function declaration. Named function is a classical declaration, and another form of named function expression leads to another type of function-named function expression (NFE).

Named function expression

The expression of a named function is the expression of a named function. Strictly speaking, it is not a type of function, but a way of writing.

/ / named function expression var fn = function foo () {console.log ('I am a named function expression');} fn () / / output: I am a named function expression

It has several characteristics:

Privatization of function name identifiers

Function name identifier constant quantization

Function name identifier is privatized: that is, the function name of a named function expression cannot be called from the outside, it is only used inside the function body, and an error will be reported when it is called externally.

/ / function name privatization var fn = function foo () {console.log (typeof foo);} fn () / / function foo () / / Uncaught ReferenceError: foo is not defined

The function name identifier is always quantized: that is, the value of the named function expression identifier cannot be modified, and we can use it as a constant.

Var fn = function foo () {foo = 12; console.log (foo);} fn () / * output: "foo () {foo = 12; console.log (foo);} * /

Let's look at a classic interview question.

/ / rewrite the following code to output 10 and 12 var foo = 10; (function foo () {foo = 12; console.log (foo);}) () / / rewrite the result var foo = 10; (function foo () {var foo = 12; console.log (window.foo); / / 10 console.log (foo); / / 12}) ()

Anonymous function

Anonymous function: that is, an anonymous function cannot declare a definition directly like a named function. The common use in JavaScript is to use an anonymous function as a callback parameter or as a return value of a higher-order function.

/ / as a callback parameter setTimeout (function () {console.log ('I am an anonymous function');}, 1000); / / the return value function foo () {var num = 10; return function (I) {return num + I;}} foo () / / output: 15

Anonymous functions have several disadvantages that we need to pay attention to:

No function name, difficult to debug

To reference itself (such as a recursive function), you need to use arguments.callee, but arguments.callee is disabled in strict mode

Poor readability

Execute the function immediately

Execute the function immediately (IIFE): that is, when the program parses to the function, it executes immediately.

/ / write method 1 (function () {console.log ('I execute the function immediately');}) () / write method II (function () {console.log ('I execute the function immediately);} ())

The advantage of anonymous functions is that internal parameters are not disclosed, that is, variables are privatized.

(function () {var x = 10 console.log ('I am executing the function immediately');} () console.log (x); / / Uncaught ReferenceError: x is not defined

There is a classic interview question that we can take a look at.

/ / rewrite the following code to output in turn. For (var I = 0; I < 5; iTunes +) {setTimeout (function () {console.log (`value is ${I}`)}, 0)} / / the "value is 5" output 5 times at this time

We will immediately think of using let, but is there any other way to write it? Of course there is. Just execute the function immediately.

/ / rewriting method 1: for (var I = 0; I < 5; iTunes +) {(function (num) {console.log (`value is ${num}`)}) (I)} / / rewriting method 2: for (var I = 0; I < 5; iTunes +) {var num = (function () {return I) }) () console.log (`value is ${num}`)} these are the functions in JavaScript that you don't know. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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