技术码头

Class 的那些事儿

2020-01-10  本文已影响0人  石菖蒲_xl

ES6 引入了Class(类)这个概念,作为对象的模板,通过class关键字,可以定义类。

class Point {
  constructor(x,y) {
    this.x = x;
    this.y = y;
  } 
}
class Point {
  constructor () {
    // do sth
  }
  toValue () {
    // do sth  
  }
}
// 等同于
Point.prototype = {
  construcror () {},
  toValue () {}
}
class B ()
let b = new B();
b.constructor === B.prototype.constructor; // true
class MyPoint{
    constructor(props){
        this.x = props.x;
        this.y = props.y;
    }
}
var myPoint = new MyPoint({x:1,y:2});
myPoint.constructor === MyPoint; // => true
MyPoint.prototype.constructor === MyPoint; // => true
  class MyPoint {
        constructor(props) {
            this.x = props.x;
            this.y = props.y;
        }
        sum() {
            console.log(this.x + this.y);
        }
    }
    var myPoint = new MyPoint({
        x: 1,
        y: 3
    });
    myPoint.sum(); // => 4
    var keys = Object.keys(MyPoint.prototype);
    console.log(keys); // => []

constructor 方法

constructor方法是默认方法,通过new命令生成对象实例时,自动调用该方法,一个类必须有constructor方法,如果没有显示定义,一个空的constructor方法会被默认添加。

  class MyPoint {
    
  }
  // 等同于
  class MyPoint {
     constructor(){}
  }
  class Foo{
        constructor(){
            var p = {x:'scp'};
            return Object.create(p);  // 返回一个全新的对象
        }
    }
    // 实例对象不是Foo类的实例
    console.log(new Foo instanceof Foo); // => false
    // 查询实例对象的原型对象是 上边定义的 p
    console.log(Object.getPrototypeOf(new Foo)); // => {x:"scp"}

类的实例

  class MyPoint {
        constructor(props) {
            this.x = props.x;
            this.y = props.y;
        }
        sum() {
            console.log(this.x + this.y);
        }
    }
    var myPoint = new MyPoint({
        x: 1,
        y: 3
    });
    console.log(myPoint.hasOwnProperty('x')); // => true
    console.log(myPoint.hasOwnProperty('sum'));// =>  false
    console.log(myPoint.__proto__.hasOwnProperty('sum')); // => true

如上,x是实例对象myPoint自身的属性,sum是定义在原型对象上的属性


继承

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

  class MyPoint {
        constructor(props) {
            this.x = props.x;
            this.y = props.y;
        }
        sum(...values) {
            console.log(values);
            let summation = values.reduce((prev,cur) => prev + cur,0);
            console.log(summation);
        }
  }
  // 继承
  class ChildrenPoint extends MyPoint{
        constructor(props,cProps){
            super(props);  // 调用父类的constructor(props)
            this.w = props.w
            this.z = cProps.z;
        }
   }
   var cPoint = new ChildrenPoint({x:1,y:2,w:3},{z:4});
   cPoint.sum(...Object.values(cPoint)); // => 10

Object.getPrototypeOf()

该方法可以用来从子类上获取父类,可以使用这个方法判定,一个类是否继承了另一个类

console.log(Object.getPrototypeOf(ChildrenPoint) === MyPoint); // => true

super 关键字

class A()
class B extends A {
  constructor() {
    super();
  }
}

上面代码,super()在这里相当于A.prototype.constructor.call(this)

  class MyPoint {
        constructor(props) {
            this.x = props.x;
        }
        sum(...values) {
            console.log(values);
            let summation = values.reduce((prev,cur) => prev + cur,0);
            console.log(summation);
        }
  }
  // 继承
  class ChildrenPoint extends MyPoint{
        constructor(props,cProps){
            super(props);  // 调用父类的constructor(props)
            this.y = cProps.y;
        }
        csum(...values){         
           super.sum(...values); //   super作为对象使用,相当于MyPoint.prototype
        }
   }
   var cPoint = new ChildrenPoint({x:1},{y:4});
   cPoint.sum(...Object.values(cPoint)); // => 10
   cPoint.csum(...Object.values(cPoint)); // => 10

上面代码中,子类ChildrenPoint当中的super.sum(...values),就是将super当做一个对象使用,super在普通方法中,指向MyPoint.prototype,所以super.sum(...values)就相当于MyPoint.prototype.sum(...values)

因为super指向父类的原型对象,所以定义在父类实例上的方法或者属性,是无法通过super调用的,比如super.x就会输出undefined

类的 prototype 属性和__proto__属性

class A {
}

class B extends A {
}

B.__proto__ === A // true
B.prototype.__proto__ === A.prototype // true
上一篇下一篇

猜你喜欢

热点阅读