ECMAScript 2022 会有哪些新特性?

console.log(Shape.color); // blue

console.log(Shape.getColor()); // blue

console.log(‘color’ in Shape); // true

console.log(‘getColor’ in Shape); // true

console.log(‘getMessage’ in Shape); // false

实例不能访问静态字段和方法:

const shapeInstance = new Shape();

console.log(shapeInstance.color); // undefined

console.log(shapeInstance.getColor); // undefined

console.log(shapeInstance.getMessage());// color:undefined

静态字段只能通过静态方法访问:

console.log(Shape.getColor()); // blue

console.log(Shape.getMessage()); //TypeError: Shape.getMessage is not a function

这里的 Shape.getMessage() 就报错了,这是因为 getMessage 不是一个静态函数,所以它不能通过类名 Shape 访问。可以通过以下方式来解决这个问题:

getMessage() {

return color:${Shape.color} ;

}

静态字段和方法是从父类继承的:

class Rectangle extends Shape { }

console.log(Rectangle.color); // blue

console.log(Rectangle.getColor()); // blue

console.log(‘color’ in Rectangle); // true

console.log(‘getColor’ in Rectangle); // true

console.log(‘getMessage’ in Rectangle); // false

4. 静态私有字段和方法

与私有实例字段和方法一样,静态私有字段和方法也使用哈希 (#) 前缀来定义:

class Shape {

static #color = ‘blue’;

static #getColor() {

return this.#color;

}

getMessage() {

return color:${Shape.#getColor()} ;

}

}

const shapeInstance = new Shape();

shapeInstance.getMessage(); // color:blue

私有静态字段有一个限制:只有定义私有静态字段的类才能访问该字段。这可能在我们使用 this 时导致出乎意料的情况:

class Shape {

static #color = ‘blue’;

static #getColor() {

return this.#color;

}

static getMessage() {

return color:${this.#color} ;

}

getMessageNonStatic() {

return color:${this.#getColor()} ;

}

}

class Rectangle extends Shape {}

console.log(Rectangle.getMessage()); // Uncaught TypeError: Cannot read private member #color from an object whose class did not declare it

const rectangle = new Rectangle();

console.log(rectangle.getMessageNonStatic()); // TypeError: Cannot read private member #getColor from an object whose class did not declare it

在这个例子中,this 指向的是 Rectangle 类,它无权访问私有字段 #color。当我们尝试调用 Rectangle.getMessage() 时,它无法读取 #color 并抛出了 TypeError。可以这样来进行修改:

class Shape {

static #color = ‘blue’;

static #getColor() {

return this.#color;

}

static getMessage() {

return ${Shape.#color};

}

getMessageNonStatic() {

return color:${Shape.#getColor()} color;

}

}

class Rectangle extends Shape {}

console.log(Rectangle.getMessage()); // color:blue

const rectangle = new Rectangle();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ES6(ECMAScript 2015)是JavaScript的一个重要,引入了许多新的语法和功能。以下是ES6的一些主要特性: 1. 块级作用域:引入了let和const关键字,可以在块级作用域中声明变量,解决了var关键字的作用域问题。 2. 箭头函数:使用箭头(=>)定义函数,简化了函数的写法,并且自动绑定了this。 3. 默认参数:函数参数可以设置默认值,简化了函数调用时的参数传递。 4. 模板字符串:使用反引号(`)包裹字符串,可以在字符串中插入变量和表达式,提供了更方便的字符串拼接方式。 5. 解构赋值:可以从数组或对象中提取值,并赋给变量,简化了变量的声明和赋值过程。 6. 扩展运算符:使用三个点(...)可以将数组或对象展开,方便地进行数组合并、复制和对象属性的拷贝。 7. 类和模块:引入了class关键字,可以使用面向对象的方式定义类和继承关系。同时,也支持模块化的导入和导出。 8. Promise:提供了一种更优雅的处理异步操作的方式,解决了回调地狱的问题。 9. 简化对象属性的定义:当属性名和变量名相同时,可以直接写属性名,省略冒号和赋值符号。 10. 简化迭代器和生成器:引入了for...of循环,可以更方便地遍历数组和类数组对象。同时,也支持生成器函数,可以通过yield关键字实现暂停和恢复执行。 11. 模块化的导入和导出:使用import和export关键字,可以将代码分割成多个模块,提高了代码的可维护性和复用性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值