您的位置:首页 > 技术中心 > 前端框架 >

es6 class是函数吗

时间:2022-04-11 17:02

es6 class是函数。在es6中,class(类)作为对象的模板被引入,可以通过class关键字定义类,语法为“class 类名{...};class的本质就是function(函数),它是一个语法糖,其底层是通过“构造函数”去创建的。

本教程操作环境:windows7系统、ECMAScript 6版、Dell G3电脑。

es6 class是函数。

在ES6中,class (类)作为对象的模板被引入,可以通过 class 关键字定义类。

class 的本质是 function。

它可以看作一个语法糖,其底层还是通过 构造函数 去创建的,让对象原型的写法更加清晰、更像面向对象编程的语法。

  1. function Person(name, age) {
  2. this.name = name;
  3. this.age = age;
  4. }
  5. Person.prototype.sayName = function() {
  6. return this.name;
  7. }
  8. const xiaoming = new Person('小明', 18);
  9. console.log(xiaoming);

上面代码用ES6的class实现,就是下面这样

  1. class Person {
  2. constructor(name, age) {
  3. this.name = name;
  4. this.age = age;
  5. }
  6. sayName() {
  7. return this.name;
  8. }
  9. }
  10. const xiaoming = new Person('小明', 18)
  11. console.log(xiaoming);
  12. // { name: '小明', age: 18 }
  13. console.log((typeof Person));
  14. // function
  15. console.log(Person === Person.prototype.constructor);
  16. // true

constructor方法,这就是构造方法,this关键字代表实例对象。 类的数据类型就是函数,类本身就指向构造函数。

定义类的时候,前面不需要加 function, 而且方法之间不需要逗号分隔,加了会报错。

类的所有方法都定义在类的prototype属性上面。

  1. class A {
  2. constructor() {}
  3. toString() {}
  4. toValue() {}
  5. }
  6. // 等同于
  7. function A () {
  8. // constructor
  9. };
  10. A.prototype.toString = function() {};
  11. A.prototype.toValue = function() {};

在类的实例上面调用方法,其实就是调用原型上的方法。

  1. let a = new A();
  2. a.constructor === A.prototype.constructor // true

类的实例

实例的属性除非显式定义在其本身(即定义在this对象上),否则都是定义在原型上(即定义在class上)。

注意:

1、class不存在变量提升

  1. new A(); // ReferenceError
  2. class A {}

因为 ES6 不会把类的声明提升到代码头部。这种规定的原因与继承有关,必须保证子类在父类之后定义。

  1. {
  2. let A = class {};
  3. class B extends A {}
  4. }

上面的代码不会报错,因为 B继承 A的时候,A已经有了定义。但是,如果存在 class提升,上面代码就会报错,因为 class 会被提升到代码头部,而let命令是不提升的,所以导致 B 继承 A 的时候,Foo还没有定义。

2、this的指向 类的方法内部如果含有this,它默认指向类的实例。但是,必须非常小心,一旦单独使用该方法,很可能报错。

继承

Class 可以通过extends关键字实现继承

  1. class Animal {}
  2. class Cat extends Animal { };

上面代码中 定义了一个 Cat 类,该类通过 extends关键字,继承了 Animal 类中所有的属性和方法。 但是由于没有部署任何代码,所以这两个类完全一样,等于复制了一个Animal类。 下面,我们在Cat内部加上代码。

  1. class Cat extends Animal {
  2. constructor(name, age, color) {
  3. // 调用父类的constructor(name, age)
  4. super(name, age);
  5. this.color = color;
  6. }
  7. toString() {
  8. return this.color + ' ' + super.toString(); // 调用父类的toString()
  9. }
  10. }

constructor方法和toString方法之中,都出现了super关键字,它在这里表示父类的构造函数,用来新建父类的this对象。

子类必须在 constructor 方法中调用 super 方法,否则新建实例就会报错。 这是因为子类自己的this对象,必须先通过 父类的构造函数完成塑造,得到与父类同样的实例属性和方法,然后再对其进行加工,加上子类自己的实例属性和方法。如果不调用super方法,子类就得不到this对象。

  1. class Animal { /* ... */ }
  2. class Cat extends Animal {
  3. constructor() {
  4. }
  5. }
  6. let cp = new Cat();
  7. // ReferenceError

Cat 继承了父类 Animal,但是它的构造函数没有调用super方法,导致新建实例报错。

如果子类没有定义constructor方法,这个方法会被默认添加,代码如下。也就是说,不管有没有显式定义,任何一个子类都有constructor方法。

  1. class Cat extends Animal {
  2. }
  3. // 等同于
  4. class Cat extends Animal {
  5. constructor(...args) {
  6. super(...args);
  7. }
  8. }

另一个需要注意的地方是,es5 的构造函数在调用父构造函数前可以访问 this, 但 es6 的构造函数在调用父构造函数(即 super)前不能访问 this。

  1. class A {
  2. constructor(x, y) {
  3. this.x = x;
  4. this.y = y;
  5. }
  6. }
  7. class B extends A {
  8. constructor(x, y, name) {
  9. this.name = name; // ReferenceError
  10. super(x, y);
  11. this.name = name; // 正确
  12. }
  13. }

上面代码中,子类的constructor方法没有调用super之前,就使用this关键字,结果报错,而放在super方法之后就是正确的。

父类的静态方法,也会被子类继承。

  1. class A {
  2. static hello() {
  3. console.log('hello world');
  4. }
  5. }
  6. class B extends A {
  7. }
  8. B.hello() // hello world

【相关推荐:javascript视频教程、web前端】

以上就是es6 class是函数吗的详细内容,更多请关注gxlsystem.com其它相关文章!

热门排行

今日推荐

热门手游