Home > Web Front-end > JS Tutorial > body text

About variable promotion and function promotion in javascript

不言
Release: 2018-07-04 10:18:19
Original
1241 people have browsed it

This article mainly introduces relevant information on variable promotion and function promotion in JavaScript. Friends who need it can refer to it

1There are only two scopes in js

a: Global scope

b: Function scope

Before ES6, js did not have block-level scope.

First of all, let’s explain what there is no block-level scope?

So at this time, you can print out the value of variable a.

2: What is variable promotion?

In our js, the code is executed in two steps, 1. Parsing 2. Step by step execution

Then variable promotion means that the variable declaration will be promoted to the top of the scope, that is, the variable will be promoted to the top of the scope no matter where it is declared.

Then the above writing method is actually equivalent to the following writing method:

Look at a few examples:

Slightly change the above example:

The result will be very different,

Let’s look at another example:

3: What is function promotion?

Output The result is:

#Note: The function declaration will raise the declaration and definition of the function to the top of the scope.

If it is written this way: a function declared by a function expression

Example:

The output result is:

Final summary:

1: All declarations will be promoted to the top of the scope .

2: The same variable will only be declared once, and others will be ignored.

3: The priority of function declaration is higher than the priority of variable declaration, and the function declaration and function definition parts are promoted together.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Introduction to the constructor pattern in JavaScript design patterns

jQuery prevents event bubbling example analysis

The above is the detailed content of About variable promotion and function promotion in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!