Классы Javascript. ES6 Classes
by Artem
JavaScript
'use strict';
class Polygon {
constructor(height, width) {
this.height = height;
this.width = width;
}
static helloPolygon() {
return 'Hello, Polygon!';
}
static get a() {
return 5;
}
}
class Square extends Polygon {
constructor(sideLength) {
super(sideLength, sideLength); // Вызываем конструктор родительского класса
}
get area() { // Геттер
return this.height * this.width;
}
set sideLength(newLength) { // Сеттер
this.height = newLength;
this.width = newLength;
}
static helloSquare() { // Статический метод
var tmp = super.helloPolygon(); // Обращение к родительскому классу
return 'Hello, Square! ' + tmp;
}
}
var square = new Square(2);
console.dir(Square.helloSquare());
// Использование дескриптора в объекте Object.create();
//var O = function() {
//
//};
//O.prototype = Object.create(Object.prototype, {
// foo: { writable: true, configurable: true, value: 'привет' },
// bar: {
// configurable: false,
// get: function() { return 10; },
// set: function(value) { console.log('Установка `o.bar` в', value); }
// }
//});
//
//var o = new O();
//console.dir(o);