寄生组合式继承

116 阅读1分钟
<script type="text/javascript">
  'user strict';
  function Car(color) {
    this.color = color
  }
  Car.myName = '父类名称'
  Car.prototype.x = function () {
    console.log('父类方法')
  }
  function Test(color) {
    Car.call(this, color)
  };
  Test.prototype = Object.create(Car.prototype, {
    constructor: {
      value: Test,
      writeable: false
    },
    bind: {
      value: function() {}  // 可以写成双向绑定
    }
  });
  var staticKeys = Object.entries(Car);
  for (let index = 0; index < staticKeys.length; index++) {
    let key = staticKeys[index][0];
    let value = staticKeys[index][1];
    Test[key] = value
  };
  const test = new Test('red');
  console.log(test);
</script>