# ES6 类

class ArrayKey {
  constructor(name = '999') {
    this.name = name;
  }

  a() {
    console.log(this.name);
  }

  b = () => {
    console.log(this.name);
  };

  render() {

  }
}

constructor, a, render方法都会放在原型对象上 b会放在this实例上

翻译后的代码

"use strict";

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 _classCallCheck(instance, Constructor) {
  if (!(instance instanceof Constructor)) {
    throw new TypeError("Cannot call a class as a function");
  }
}

var ArrayKey = (function() {
  function ArrayKey() {
    var _this = this;

    var name =
      arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "999";

    _classCallCheck(this, ArrayKey);

    this.b = function() {
      console.log(_this.name);
    };

    this.name = name;
  }

  _createClass(ArrayKey, [
    {
      key: "a",
      value: function a() {
        console.log(this.name);
      }
    },
    {
      key: "render",
      value: function render() {}
    }
  ]);

  return ArrayKey;
})();
陕ICP备20004732号-3