摘要:
类相当于实例的原型,所有在类中定义的方法,都会被实例继承。如果在一个方法前,加上static关键字,就表示该方法不会被实例继承,而是直接通过类来调用,这就称为“静态方法”。 class Foo { static classMethod() { return 'hello'; } } Foo.clas 阅读全文
posted @ 2020-03-25 16:20
banzhuxiang
阅读(359)
评论(0)
推荐(0)
摘要:
严格模式 类和模块的内部,默认就是严格模式,所以不需要使用use strict指定运行模式。只要你的代码写在类或模块之中,就只有严格模式可用。考虑到未来所有的代码,其实都是运行在模块之中,所以 ES6 实际上把整个语言升级到了严格模式 不存在提升 类不存在变量提升(hoist),这一点与 ES5 完 阅读全文
posted @ 2020-03-25 16:15
banzhuxiang
阅读(192)
评论(0)
推荐(0)
摘要:
类的属性名,可以采用表达式。 let methodName = 'getArea'; class Square { constructor(length) { // ... } [methodName]() { // ... } } 上面代码中,Square类的方法名getArea,是从表达式得到的 阅读全文
posted @ 2020-03-25 16:08
banzhuxiang
阅读(479)
评论(0)
推荐(0)
摘要:
与 ES5 一样,在“类”的内部可以使用get和set关键字,对某个属性设置存值函数和取值函数,拦截该属性的存取行为。 class MyClass { constructor() { // ... } get prop() { return 'getter'; } set prop(value) { 阅读全文
posted @ 2020-03-25 16:02
banzhuxiang
阅读(652)
评论(0)
推荐(0)
摘要:
生成类的实例的写法,与 ES5 完全一样,也是使用new命令。如果忘记加上new,像函数那样调用Class,将会报错。 class Point { // ... } // 报错 var point = Point(2, 3); // 正确 var point = new Point(2, 3); 与 阅读全文
posted @ 2020-03-25 15:59
banzhuxiang
阅读(317)
评论(0)
推荐(0)
摘要:
constructor方法是类的默认方法,通过new命令生成对象实例时,自动调用该方法。一个类必须有constructor方法,如果没有显式定义,一个空的constructor方法会被默认添加。 class Point { } // 等同于 class Point { constructor() { 阅读全文
posted @ 2020-03-25 15:38
banzhuxiang
阅读(1312)
评论(0)
推荐(0)
摘要:
class关键字,可以定义类 function Point(x, y) { this.x = x; this.y = y; } Point.prototype.toString = function () { return '(' + this.x + ', ' + this.y + ')'; }; 阅读全文
posted @ 2020-03-25 15:34
banzhuxiang
阅读(149)
评论(0)
推荐(0)
摘要:
回调函数 事件监听 发布/订阅 Promise 对象 阅读全文
posted @ 2020-03-25 15:23
banzhuxiang
阅读(90)
评论(0)
推荐(0)
摘要:
由于 Generator 函数返回的遍历器对象,只有调用next方法才会遍历下一个内部状态,所以其实提供了一种可以暂停执行的函数。yield表达式就是暂停标志。 function* gen() { yield 123 + 456; } 上面代码中,yield后面的表达式123 + 456,不会立即求 阅读全文
posted @ 2020-03-25 14:55
banzhuxiang
阅读(202)
评论(0)
推荐(0)
摘要:
Generator 函数是 ES6 提供的一种异步编程解决方案,语法行为与传统函数完全不同 Generator 函数是一个普通函数,但是有两个特征。一是,function关键字与函数名之间有一个星号;二是,函数体内部使用yield表达式,定义不同的内部状态(yield在英语里的意思就是“产出”) f 阅读全文
posted @ 2020-03-25 14:49
banzhuxiang
阅读(227)
评论(0)
推荐(0)