ES6 (ES2015) 如何演变并为现代 JavaScript 带来新功能?

在本文中,您将了解 ES6 (ES2015) 如何发展并为现代 JavaScript 带来新功能。
ES6 代表 ECMAScript 6。它是 ECMAScript 的第 6 个版本,旨在标准化 JavaScript。 ES6 的十大功能是:let 和 const 关键字、箭头函数、多行字符串、默认参数、模板文字、解构赋值、增强的对象文字、Promise。
示例 1
在这个例子中,我们来演示箭头函数(=>) -
console.log("An Arrow function Square has been defined")
square = (x) => { return x * x; }
let inputValue = 6
console.log("The input value is defined as :", inputValue)
let result= square(inputValue)
console.log("The square of the given value is :", result)
说明
第 1 步 - 定义一个箭头函数“square”,它接受一个数字作为参数并返回该数字的平方。
第 2 步 - 调用函数并将函数调用分配给一个值:“result”;
第 3 步 - 显示结果。
示例 2
console.log("A class Rectangle is defined that uses constructor ",)
class Rectangle {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
let object = new Rectangle(10, 20);
console.log("A sides of the rectangle are defined as ")
console.log(object.x, " and ",object.y)
说明
步骤 1 - 定义一个“矩形”类,它接受两个数字作为参数。该类使用构造函数分配两个值。
第 2 步 - 使用对象调用类。
第 3 步 - 显示值。
javascript