Skip to content

Instantly share code, notes, and snippets.

@zandroid
Created May 31, 2013 19:30
Show Gist options
  • Save zandroid/5687392 to your computer and use it in GitHub Desktop.
Save zandroid/5687392 to your computer and use it in GitHub Desktop.
function Rect(color, width, height) {
// определяем геттеры
this.color = function() {
return color;
};
this.width = function() {
return width;
};
this.height = function() {
return height;
};
// определяем и методы в замыкании, чтобы не зависеть от замены геттеров в инстансе
this.s = function() {
return width * height;
};
this.p = function() {
return 2 * (width + height);
};
}
// в прототип можем поместить методы, которые не зависят от color, width, height
Rect.prototype.x = 0; // значение по умолчанию
Rect.prototype.y = 0; // значение по умолчанию
Rect.prototype.setPosition = function(x,y) {
this.x = x;
this.y = y;
};
function Square(color, width) {
// вызываем базовый конструктор
Rect.call(this, color, width, width);
// после чего получаем лишний по условию задачи геттер this.height(), с которым расправляемся так:
delete this.height;
}
// наследование
Square.prototype = Object.create(Rect.prototype);
Square.prototype.constructor = Square;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment