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

Can var be used to declare variables in es6

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains the "es6 can be used to declare variables in var", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "es6 can use var to declare variables" bar!

Es6 can declare variables in var. Var is a keyword that declares variables. Variables declared using the var keyword have two scopes: global scope and function scope; because var supports variable promotion, global variables declared with var are valid throughout script code, and local variables declared with var are valid throughout functions.

The operating environment of this tutorial: windows7 system, ECMAScript version 6, Dell G3 computer.

ES5 has only two ways to declare variables: the var command and the function command.

In addition to adding let and const commands, ES6 has two ways to declare variables: the import command and the class command.

Therefore, there are six ways to declare variables in ES6-1:

Declare variables with the keyword var

Declare variables with the keyword function

Declare variables with the keyword const

Declare variables with the keyword let

Declare variables with the keyword class

Declare variables with the keyword import

Declare variables with the keyword var

Var is a keyword for declaring variables, declaring methods:

Var variable name

To give a few examples:

Var str; / / to store the string var age; / / to store the age var prePage; / / to store the previous page

When defining variables, you can define one or more variables at a time. If you define more than one variable, you need to separate the variable names with commas, as shown in the following example:

Var a, b, c; / / declare multiple variables at the same time

After the variable is defined, if the variable is not assigned a value, then the variable is assigned an initial value-undefined (undefined).

After the variable is defined, you can assign a value to the variable using the equal sign =. The one to the left of the equal sign is the name of the variable, and the right side of the equal sign is the value to be assigned to the variable, as shown in the following example:

Var num; / / defines a variable numnum = 1; / / assigns the variable num to 1

Alternatively, you can assign values to variables while defining them, as shown in the following example:

Var num = 1; / / define a variable num and assign it to 1var a = 2, b = 3, c = 4; / / define three variables a, b, c at the same time and assign values as 2, 3, 4 var a = 2, / / to make the code look neater, the previous line of code can also be written like this / b = 3.

Description:

Variables defined by var can be modified. If not initialized, undefined will be output and no error will be reported.

The variable declared by var is on window. Use let or const to declare the variable. This variable will not be placed on window.

There are block-level scopes in many languages, but JS does not. It uses var to declare variables and demarcates scopes by function. Curly braces "{}" do not limit the scope of var, so variables declared with var have the effect of variable promotion.

The variable scope declared by var is global or functional

Expand knowledge:

The let and const keywords declare variables

Before 2015, JavaScript can only declare variables through the var keyword. After the release of ECMAScript6 (ES6), two new keywords, let and const, are added to declare variables, where:

Variables declared with the let keyword are only valid in the code block in which they are located (similar to local variables), and variables with the same name cannot be repeatedly declared in this code block

The function of the const keyword is the same as that of let, but variables declared using the const keyword also have another feature: variables defined by the const keyword cannot be modified once defined (that is, variables defined using the const keyword are constant).

Note: the let and const keywords are not supported in IE10 and below.

The sample code is as follows:

Let name = "Xiaoming"; / / declare a variable name and assign it to "Xiaoming" let age = 11; / / declare a variable agelet age = 13; / / error: variable age cannot repeatedly define const PI = 3.1415 / / declare a constant PI and assign it to 3.1415console.log (PI) / / print PI on the console

Scope of var, let, and const

Variables declared with var have global scope and function scope; variables declared with let and const have global scope, local scope, and block scope.

Because var supports variable promotion, the global scope of var variables is valid for script code throughout the page, while let and const do not support variable promotion, so the global scope of let and const variables refers to the entire area from the beginning of the declaration statement to the end of the script code of the entire page, while the area before the declaration statement is not valid.

Similarly, because var supports variable promotion, while let and const do not, local variables declared with var are valid throughout the function, while local variables declared with let and const are valid from the beginning of the declaration statement to the end of the function. It should be noted that if the local variable and the global variable have the same name, then in the function scope, the local variable will cover the global variable, that is, it is the local variable that plays a role in the function body; outside the function, the global variable works, the local variable is invalid, and a syntax error will occur when referencing the local variable.

Declare variables with the keyword function

Function add (a) {var sum = a + 1; return sum;}

A new variable named add is declared and assigned a function definition

The content between {} is assigned to add

The code inside the function is not executed, but is stored in variables for future use

Declare variables with the keyword class

Later on, I will write a separate article about class, which will be introduced in detail. Here is a brief introduction: first, look at the constructor in ES5, and then use ES6's class to implement it:

/ / ES5: function fun (x, y) {this.x = x; this.y = y;}; fun.prototype.GetHair = function () {return `${this.x} lost two hairs, ${this.y} said me too! `;}; let setHair = new fun ('Xiaoming', 'Lao Wang'); console.log (setHair.GetHair ()); / / Xiaoming lost two hairs, Lao Wang said I was the same!

Let's take a look at ES6's class writing:

Class Interest {constructor (x, y, e, z) {this.x = x; this.y = y; this.e = e; this.z = z;} MyInterest () {let arr = []; console.log (`I will ${[... arr,this.x,this.y,this.e,this.z]}! `) } let GetInterest = new Interest ('sing', 'jump', 'rap',' basketball'); console.log (GetInterest.MyInterest ()); / / I can sing, dance, rap, basketball!

ES6's class can be regarded as a syntax sugar, most of its functions can be done by ES5, the new class writing only makes the object prototype written more clearly, more like the syntax of object-oriented programming, the constructor method is the construction method, and the this keyword represents the instance object. This is the constructor Point of ES5, which corresponds to the constructor of the Point class of ES6.

Declare variables with the keyword import

Import is used to load files, receiving one or more variable names in curly braces, which need to be the same as the variable names you want to import.

For example, Chestnut: you want to import a variable in the action.js file, and a block of code is saved in this variable, so you have to write: import {Button} from 'action', you get a variable called Button from action.js, and now that you have it, you can obscene the code in Button for

If you want to rename the entered variable, the import command uses the as keyword to rename the entered variable, such as:

Import {NewButton as Button} from 'action.js'

The above code shows that you get a code snippet with the name of a variable called Button from the action.js file, and then you declare a variable NewButton to save the Button in NewButton

Thank you for your reading, the above is the content of "can you declare variables with var in es6?" after the study of this article, I believe you can use var to declare variables in es6 have a deeper understanding of this problem, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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