Es6-features 讀書心得

Classes

  • 是基於prototype的語法糖

建構class

class Polygon {
    constructor(height, width) {
        this.height = height;
        this.width = width;
    }
}
function _classCallCheck(instance, Constructor) { 
    if (! (instance instanceof Constructor)) { 
        throw new TypeError("Cannot call a class as a function"); 
    } 
}

var Polygon = function Polygon(height, width) {
    _classCallCheck(this, Polygon);

    this.height = height;
    this.width = width;
};

Prototype methods and Static methods

class Polygon {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }

  calcArea() {
    return this.height * this.width;
  }

  static calculate(w, h) {
    return w * h;
  }
}
function _classCallCheck(instance, Constructor) { 
    if (! (instance instanceof Constructor)) { 
        throw new TypeError("Cannot call a class as a function"); 
    } 
}

var Polygon = (function () {
  function Polygon(height, width) {
    _classCallCheck(this, Polygon);

    this.height = height;
    this.width = width;
  }

  _createClass(Polygon, [{
    key: "calcArea",
    value: function calcArea() {
      return this.height * this.width;
    }
  }], [{
    key: "calculate",
    value: function calculate(w, h) {
      return w * h;
    }
  }]);

  return Polygon;
})();

extends

class Animal { 
  constructor(name) {
    this.name = name;
  }

  speak() {
    console.log(this.name + ' makes a noise.');
  }
}

class Dog extends Animal {
  speak() {
    console.log(this.name + ' barks.');
  }
}
var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };

var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();

function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }

var Animal = (function () {
  function Animal(name) {
    _classCallCheck(this, Animal);

    this.name = name;
  }

  _createClass(Animal, [{
    key: 'speak',
    value: function speak() {
      console.log(this.name + ' makes a noise.');
    }
  }]);

  return Animal;
})();

var Dog = (function (_Animal) {
  _inherits(Dog, _Animal);

  function Dog() {
    _classCallCheck(this, Dog);

    _get(Object.getPrototypeOf(Dog.prototype), 'constructor', this).apply(this, arguments);
  }

  _createClass(Dog, [{
    key: 'speak',
    value: function speak() {
      console.log(this.name + ' barks.');
    }
  }]);

  return Dog;
})(Animal);

super

class Cat { 
  constructor(name) {
    this.name = name;
  }

  speak() {
    console.log(this.name + ' makes a noise.');
  }
}

class Lion extends Cat {
  speak() {
    super.speak();
    console.log(this.name + ' roars.');
  }
}
var Cat = (function () {
  function Cat(name) {
    _classCallCheck(this, Cat);

    this.name = name;
  }

  _createClass(Cat, [{
    key: 'speak',
    value: function speak() {
      console.log(this.name + ' makes a noise.');
    }
  }]);

  return Cat;
})();

var Lion = (function (_Cat) {
  _inherits(Lion, _Cat);

  function Lion() {
    _classCallCheck(this, Lion);

    _get(Object.getPrototypeOf(Lion.prototype), 'constructor', this).apply(this, arguments);
  }

  _createClass(Lion, [{
    key: 'speak',
    value: function speak() {
      _get(Object.getPrototypeOf(Lion.prototype), 'speak', this).call(this);
      console.log(this.name + ' roars.');
    }
  }]);

  return Lion;
})(Cat);