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 − 定义一个名为 'Rectangle' 的类,它接收两个数字作为参数。该类使用构造函数来赋值两个值。
步骤 2 − 使用对象调用类。
步骤 3 − 显示值。
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP