【JavaScript】New features in 2.2 ES6 and above


The JavaScript standard is maintained by the TC39 committee of ECMA International (European Computer Manufacturers Association), and this standard is called ECMAScript. ES6, the full name of ECMAScript 2015, is an important version of JavaScript that introduces many new features. In this chapter, we will learn about some important new features of ES6 and above.

let and const

ES6 introduced the let and const keywords to declare variables. The variable declared by let has block-level scope, while the variable declared by const is a constant, which cannot be changed once assigned.

let x = 10; // 声明一个变量
x = 20; // 可以改变变量的值

const y = 30; // 声明一个常量
y = 40; // 错误:不能改变常量的值

arrow function

ES6 introduced arrow functions, which have a cleaner syntax and do not bind their ownthis.

// 普通函数
var square1 = function(x) {
    
    
  return x * x;
};

// 箭头函数
var square2 = x => x * x;

console.log(square1(5)); // 输出:25
console.log(square2(5)); // 输出:25

In this example, the square1 and square2 functions have the same function, but square2 uses the arrow function syntax.

template string

ES6 introduced template strings, which can contain placeholders, represented by ${}. Template strings are surrounded by backticks (`).

var name = 'Alice';
var greeting = `Hello, ${
      
      name}!`;
console.log(greeting); // 输出:Hello, Alice!

In this example, ${name} is a placeholder that will be replaced by the value of the name variable.

Destructuring assignment

ES6 introduces destructuring assignment, which can destructure the value of an array or object into a variable.

// 数组解构
var [a, b] = [1, 2];
console.log(a); // 输出:1
console.log(b); // 输出:2

// 对象解构
var {
    
    x, y} = {
    
    x: 10, y: 20};
console.log(x); // 输出:10
console.log(y); // 输出:20

In this example, we use destructuring assignment to assign array and object values ​​to variables.

Promise

ES6 introduces Promise, which is a solution for asynchronous programming. Promise has three states: pending (waiting), fulfilled (successful) and rejected (failed).

var promise = new Promise(function(resolve, reject) {
    
    
  setTimeout(function() {
    
    
    resolve('Hello, world!');
  }, 1000);
});

promise.then(function(value) {
    
    
  console.log(value); // 输出:Hello, world!
}).catch(function(error) {
    
    
  console.error(error);
});

In this example, we create a new Promise. This Promise will succeed after 1 second, and the value will be 'Hello, world!'. We then use the then method to handle successful results.

kind

ES6 introduced classes, which are syntactic sugar for object-oriented programming. Classes can contain constructors, methods, and static methods.

class Person {
    
    
  constructor(name) {
    
    
    this.name = name;
  }

  sayHello() {
    
    
    console.log('Hello, my name is ' + this.name);
  }

  static greet() {
    
    
    console.log('Hello, world!');
  }
}

var alice = new Person('Alice');
alice.sayHello(); // 输出:Hello, my name is Alice
Person.greet(); // 输出:Hello, world!

In this example, we define a Person class and then create an instance of Person.

Summarize

The new features of ES6 and above allow us to write simpler and more powerful JavaScript code. By understanding and using let, const, arrow functions, template strings, destructuring assignment, Promise and classes, we can better master JavaScript and develop in practice. apply this knowledge.

In the next chapters, we will learn more about JavaScript in depth. I hope you can find fun in the learning process and start your programming journey by learning JavaScript!
Insert image description here

Guess you like

Origin blog.csdn.net/u010671061/article/details/134454784