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

Example Analysis of JS function

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Most people do not understand the knowledge points of this "JS function instance Analysis" article, so the editor summarizes the following, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "JS function instance Analysis" article.

What is the difference between a function declaration and a function expression

Function declaration: functionbar () {}

Function expression: varfuc=foo () {}

1. The function bar will be hoist before the entire program is executed, so it is available throughout the scope (scope) of the defined bar function. It's okay to call a function even before it's defined.

two。 For function declarations, the name of the function is required, while for function expressions it is optional, so anonymous and named function expressions appear.

3 in this function

Varfoo=functionbar () {

Bar (); / / Works}

The bar (); / ReferenceError naming function bar is assigned to the variable foo, so it is not visible outside the function declaration, but it can still be called inside the bar` function. This is because of the mechanism that Javascript handles naming functions, and the name of the function is always valid within the scope of the function.

What is the declaration prefix of a variable? What is the declaration prefix of a function (*)

The so-called variable declaration prefix is in a scope block where all variables are declared at the beginning of the block. Here are three examples of code:

Code 1:

Vara=1

Functionmain () {

Console.log (a); / / 1

}

Main (); / / output 1`

Code 2:

1vara=1

2functionmain () {

3console.log (a)

4vara=2

5}

6main () / / output undefined

Code 3: why Code 2 outputs undefined

1vara=1

2functionmain () {

3vara

4console.log (a)

5a=2

6}

The declaration front of a function promotes the entire function to the front of the current scope (after the preceding variable declaration).

Varnum=1

Console.log (doubleNum (num)); / / 2

FunctiondoubleNum (num) {returnnum2;}

Equivalent to

/ / the declaration of the function is preceded

Varnum

FunctiondoubleNum (num) {returnnum2;}

Num=1

Console.log (doubleNum (num)); / / 2

What is arguments (*)

Arguments is a class array object. Represents a list of parameters passed to a function. You can get all the parameters of the function inside the function by using the arguments object. This object creates an entry for each parameter passed to the function, with the index number of the entry starting at 0.

How to implement the overloading of functions (*)

This can be done through the length of the arguments and the type of the parameter. Example:

FunctionsumOfSquares (a _ b _ c) {

If (arguments.length

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