Table of Contents
3. Global block scope binding
Home Web Front-end Front-end Q&A Is es6 syntax a standard?

Is es6 syntax a standard?

Oct 21, 2022 pm 04:38 PM
javascript es6

es6 syntax is a standard. The full name of ES6 is ECMAScript 6, which is an officially released standard for the JavaScript language. The goal of this standard is to enable the JavaScript language to be used to write complex large-scale applications and become an enterprise-level development language. The relationship between ECMAScript and JavaScript is: the former is a specification of the latter, and the latter is an implementation of the former.

Is es6 syntax a standard?

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

The full name of ES6 is ECMAScript 6, which is an officially released standard for the JavaScript language. The goal of this standard is to enable the JavaScript language to be used to write complex large-scale applications and become an enterprise-level development language.

ES6 is a new generation standard of JavaScript language released after ES5, adding many new features and syntax. The standard was released as an official version on June 17, 2015, and was officially named ES2015.

The relationship between ECMAScript and JavaScript is that the former is a specification of the latter, and the latter is an implementation of the former (other ECMAScript dialects include JScript and ActionScript)

2011 , after the release of ECMAScript version 5.1, the development of version 6.0 began. Therefore, the original meaning of the word ES6 refers to the next version of the JavaScript language. The first version of ES6 was released in June 2015, and its official name is "ECMAScript 2015 Standard" (ES2015 for short). In June 2016, the slightly revised "ECMAScript 2016 Standard" (referred to as ES2016) was released as scheduled. This version can be regarded as the ES6.1 version, because the difference between the two is very small and they are basically the same standard. According to the plan, the ES2017 standard will be released in June 2017.

Therefore, ES6 is both a historical term and a general term. It means the next generation standard of JavaScript after version 5.1, covering ES2015, ES2016, ES2017, etc., while ES2015 is the official name, specifically referring to The official version of the language standard released that year. When we talk about ES6, we generally refer to the ES2015 standard, but sometimes we also refer to the "next generation JavaScript language" in general.

1. Block scope constructs let and const

Block scope exists inside: function , block (ie: the area between the characters " { " and " } ")

1.let statement

  • Declared through var There is a variable promotion mechanism for variables, but variables declared by let will not be promoted. The scope of the variable can be limited to the current code block
//通过var声明的变量
  //函数内部
        function changeState(flag) {
            if (flag) {
                var color = "red"
            } else {
                console.log(color);
                return null;
            }
        }
        changeState(false);
   //块中
        {
            var a = 1;
        }
        console.log("a=" + a);
   //for循环中
        for (var i = 0; i <p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/image/162/737/761/1666341173585140.png" class="lazy" title="1666341173585140.png" alt="Is es6 syntax a standard?"></p><pre class="brush:php;toolbar:false"> //通过let声明的变量
            //函数内部
            function changeState(flag) {
                if (flag) {
                    let color = "red"
                } else {
                    console.log(color);
                    return null;
                }
            }
            changeState(false);
       
            //块中
            {
                let a = 1;
            }
            console.log("a=" + a);
      
            //for循环中
            for (let i = 0; i <p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/024/ae4e9436b32da42a0fd6ccd91f3142c6-3.png" class="lazy" title="166634117986532Is es6 syntax a standard?" alt="Is es6 syntax a standard?"></p>
Copy after login
  • In the same scope, you cannot use let to repeatedly declare an existing identifier, but if it is in a different scope, it is possible.
// 在同一作用域下,不能使用let重复声明已经存在的标识符,但如果在不同的作用域下,则是可以的
    var a=0;
    var b=0;
    {
        let a=0;
    }
    let b=0;
Copy after login

Is es6 syntax a standard?

  • Using let to declare variables can prevent repeated declaration of variables
 		var a=0;
        var a=10;//ok
        var b=1
        let b=100;
Copy after login

Is es6 syntax a standard?

2.const declaration

  • Every variable declared through the const keyword must be initialized at the same time as the declaration
  • Use const in the same scope Declaring an existing identifier will also cause a syntax error
  • Use const to declare an object. The binding of the object itself cannot be modified, but the properties and values ​​of the object can be modified
   	const person={
            name:"zhangSan"
        };
        person.name="lisi";	 //ok
        person.age=19;	//ok
        
        person={
            name:"wangwu"
        };
Copy after login

Is es6 syntax a standard?

3. Global block scope binding

  • Variables or objects declared using var in the global scope will be used as attributes of the window object in the browser environment (Using var is likely to inadvertently overwrite an existing global property)
 		var greeting="welcome";
        console.log(window.greeting);
        console.log(window.Screen);
        var Screen="liquid crystal";
        console.log(window.Screen);
Copy after login

Is es6 syntax a standard?

  • Use let or const to declare variables and constants to avoid overwriting window Properties of objects
 		let greeting="welcome";
        console.log(window.greeting);
        console.log(window.Screen);
        const Screen="liquid crystal";
        console.log(window.Screen==Screen);
Copy after login

Is es6 syntax a standard?

Summary

  • 通过var声明的变量存在变量提升机制,而let声明的变量不会被提升,可将变量的作用域限制在当前代码块中
  • 在同一作用域下,不能使用let重复声明已经存在的标识符,但如果在不同的作用域下,则是可以的
  • 使用let声明变量,可以防止变量的重复声明
  • 每个通过const关键字声明的变量必须在声明的同时进行初始化
  • 在同一作用域下用const声明已经存在的标识符也会导致语法错误
  • 使用const声明对象,对象本身的绑定不能修改,但对象的属性和值是可以修改的
  • 在全局作用域中使用var声明的变量或对象,将作为浏览器环境中的window对象的属性(使用var很可能会无意中覆盖一个已经存在的全局属性)
  • 使用let或const声明变量和常量,避免覆盖window对象的属性

二、解构赋值

解构赋值是对赋值运算符的扩展。

他是一种针对数组或者对象进行模式匹配,然后对其中的变量进行赋值。

在代码书写上简洁且易读,语义更加清晰明了;

也方便了复杂对象中数据字段获取。

//1、数组解构
// 传统
let a = 1, b = 2, c = 3
console.log(a, b, c)
// ES6
let [x, y, z] = [1, 2, 3]
console.log(x, y, z)
/*********************************************************************************************************/
/*********************************************************************************************************/
//2、对象解构
let user = {name: &#39;Johon&#39;, age: 18}
// 传统
let name1 = user.name
let age1 = user.age
console.log(name1, age1)
// ES6
let { name, age } = user//注意:解构的变量必须和user中的属性同名
console.log(name, age)
Copy after login

三、模板字符串

模板字符串相当于加强版的字符串,用反引号 `,除了作为普通字符串,还可以用来定义多行字符串,

还可以在字符串中加入变量和表达式。

// 字符串插入变量和表达式。变量名写在 ${} 中,${} 中可以放入 JavaScript 表达式。
let name = &#39;Kuangshen&#39;
let age = 27
let info = `My Name is ${name},I am ${age+1} years old next year.`
console.log(info)
// My Name is Kuangshen,I am 28 years old next year.
Copy after login

四、声明对象简写

const age = 12
const name = &#39;小王&#39;
// 传统
const person1 = {age: age, name: name}
console.log(person1)
// ES6
const person2 = {age, name}
console.log(person2) //{age: 12, name: &#39;小王&#39;}
Copy after login

五、定义方法简写

// 传统
const person1 = {
sayHi:function(){
console.log(&#39;Hi&#39;)
}
}
person1.sayHi();//&#39;Hi&#39;
// ES6
const person2 = {
sayHi(){
console.log(&#39;Hi&#39;)
}
}
person2.sayHi() //&#39;Hi&#39;
Copy after login

六、对象拓展运算符

符号 (...)

let person = {nameL:"oAk",age:23}
let someone1 = persion // 引用赋值
let someone2 = { ...person } // 对象拷贝
someone1.name = &#39;oAk_OLD&#39;
someone2.name = &#39;oAk_NEW&#39;
console.log(persion) // {name:&#39;oAk_OLD&#39;, age:23}
console.log(someone1) // {name:&#39;oAk_OLD&#39;, age:23}
console.log(someone2) // {name:&#39;oAk_NEW&#39;, age:23}
Copy after login

【相关推荐:javascript视频教程编程视频

The above is the detailed content of Is es6 syntax a standard?. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript and WebSocket: Building an efficient real-time image processing system JavaScript and WebSocket: Building an efficient real-time image processing system Dec 17, 2023 am 08:41 AM

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data

See all articles