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

How to advance the function in JavaScript

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

Share

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

It is believed that many inexperienced people don't know what to do about how to advance the function in JavaScript. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Global scope

The global scope is the largest scope.

Variables defined in the global scope can be used anywhere

When the page opens, the browser will automatically generate a global scope window for us.

This scope will exist until the page is closed and destroyed.

/ / the following two variables exist under the global scope and are var num = 100var num2 = 200that can be used anywhere.

Local scope

A local scope is a relatively small scope opened up under the global scope.

Variables defined in the local scope can only be used within this local scope

In JS, only a function can generate a local scope, nothing else

Every function is a local scope.

/ / this num is a variable in global scope. You can use var num = 100 anywhere.

Function fn () {

/ / the following variable is a variable within the local scope of fn / / you can only use var num2 = 200} inside the fn function.

Fn ()

Variable usage rules (emphasis)

Once you have a scope, variables have scope and rules.

There are two kinds of variable usage rules, access rules and assignment rules.

Access rules

When I want to get the value of a variable, we call this behavior access

Rules for getting variables:

First of all, look inside your scope, and if there is, use it directly.

If not, go to the upper scope and, if so, use it.

If not, continue to search the scope at the next level, and so on

If there is no such variable up to the global scope, it will directly report an error (the variable is not defined)

Var num = 100

Function fn () {

Var num2 = 200

Function fun () {

Var num3 = 300

Console.log (num3) / / you have it in your own scope. If you take it and use console.log (num2) / / if you don't have it in your scope, go to a higher level, that is, look for it in the scope of fn. If you find it, you can use console.log (num) / / you don't have it. You can't even go to the next level fn. If you find it in the global scope, use console.log (a) / / you don't have it. If you go up to the overall situation, you will report an error.

Fun ()}

Fn ()

The access rules of variables are also called scope lookup mechanisms.

The scope search mechanism can only look up, not down.

Function fn () {

Var num = 100} fn ()

Console.log (num) / / found that his scope is not, he is the global scope, there is no higher level, directly report an error

Assignment rule

When you want to assign a value to a variable, then you have to find the variable first and assign a value to it.

Variable assignment rules:

First find it within your own scope, and assign it directly if you have it.

If not, go to the next higher scope to find it, and assign a value directly.

If you don't go to the next level of scope to find it, you can assign it directly.

If you keep finding that there is no global scope, then define this variable as a global variable and assign it a value.

Function fn () {

Num = 100} fn ()

/ / after fn is called, you need to assign a value to num / / check that there is no num variable inside your scope / / then you will look up to the next level, that is, the global scope. If you find that there is still no / / then you will define num as a global variable and assign a value to it / / so after fn (), there will be a variable called num and the value will be 100console.log (num) / / 100.

After reading the above, have you mastered the method of how to advance the function in JavaScript? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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