Показать сообщение отдельно
  #3 (permalink)  
Старый 23.08.2015, 19:42
Аватар для KosBeg
Профессор
Отправить личное сообщение для KosBeg Посмотреть профиль Найти все сообщения от KosBeg
 
Регистрация: 22.05.2015
Сообщений: 384

первое что загуглилось - https://gist.github.com/lpsBetty/2cadbb7f2e37888ad322
/**
 * Classes and Inheritance
 * Code Example from [url]http://www.es6fiddle.net/[/url]
 */
class Polygon {
  constructor(height, width) { //class constructor
    this.name = 'Polygon';
    this.height = height;
    this.width = width;
  }

  sayName() { //class method
    console.log('Hi, I am a', this.name + '.');
  }
}

class Square extends Polygon {
  constructor(length=10) { // ES6 features Default Parameters
    super(length, length); //call the parent method with super
    this.name = 'Square';
  }

  get area() { //calculated attribute getter
    return this.height * this.width;
  }
}

let s = new Square(5);

s.sayName(); // => Hi, I am a Square.
console.log(s.area); // => 25

console.log(new Square().area); // => 100

/**
 * Classes and Inheritance
 * Code Example from [url]http://www.es6fiddle.net/[/url]
 */
'use strict';

var _get = function get(_x2, _x3, _x4) { var _again = true; _function: while (_again) { var object = _x2, property = _x3, receiver = _x4; 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 { _x2 = parent; _x3 = property; _x4 = 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) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

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);

    //class constructor
    this.name = 'Polygon';
    this.height = height;
    this.width = width;
  }

  _createClass(Polygon, [{
    key: 'sayName',
    value: function sayName() {
      //class method
      console.log('Hi, I am a', this.name + '.');
    }
  }]);

  return Polygon;
})();

var Square = (function (_Polygon) {
  _inherits(Square, _Polygon);

  function Square() {
    var length = arguments.length <= 0 || arguments[0] === undefined ? 10 : arguments[0];

    _classCallCheck(this, Square);

    // ES6 features Default Parameters
    _get(Object.getPrototypeOf(Square.prototype), 'constructor', this).call(this, length, length); //call the parent method with super
    this.name = 'Square';
  }

  _createClass(Square, [{
    key: 'area',
    get: function get() {
      //calculated attribute getter
      return this.height * this.width;
    }
  }]);

  return Square;
})(Polygon);

var s = new Square(5);

s.sayName(); // => Hi, I am a Square.
console.log(s.area); // => 25

console.log(new Square().area); // => 100
Ответить с цитированием