var Rectangle = function(width, height) {
this.width = width;
this.height = height;
};
Rectangle.prototype.getArea = function() {
return this.width * this.height;
};
Rectangle.isRectangle = function(instance) {
return (
instance instanceof Rectangle && instance.width > 0 && instance.height > 0
);
};
var rect1 = new Rectangle(3, 4);
console.log(rect1.getArea()); // 12 (O)
console.log(rect1.isRectangle(rect1)); // Error (X)
console.log(Rectangle.isRectangle(rect1)); // true
var Grade = function() {
var args = Array.prototype.slice.call(arguments);
for (var i = 0; i < args.length; i++) {
this[i] = args[i];
}
this.length = args.length;
};
Grade.prototype = [];
var g = new Grade(100, 80);
var Grade = function() {
var args = Array.prototype.slice.call(arguments);
for (var i = 0; i < args.length; i++) {
this[i] = args[i];
}
this.length = args.length;
};
Grade.prototype = [];
var g = new Grade(100, 80);
g.push(90);
console.log(g); // Grade { 0: 100, 1: 80, 2: 90, length: 3 }
delete g.length;
g.push(70);
console.log(g); // Grade { 0: 70, 1: 80, 2: 90, length: 1 }
var Grade = function() {
var args = Array.prototype.slice.call(arguments);
for (var i = 0; i < args.length; i++) {
this[i] = args[i];
}
this.length = args.length;
};
Grade.prototype = ['a', 'b', 'c', 'd'];
var g = new Grade(100, 80);
g.push(90);
console.log(g); // Grade { 0: 100, 1: 80, 2: 90, length: 3 }
delete g.length;
g.push(70);
console.log(g); // Grade { 0: 100, 1: 80, 2: 90, ___ 4: 70, length: 5 }
var Rectangle = function(width, height) {
this.width = width;
this.height = height;
};
Rectangle.prototype.getArea = function() {
return this.width * this.height;
};
var rect = new Rectangle(3, 4);
console.log(rect.getArea()); // 12
var Square = function(width) {
this.width = width;
};
Square.prototype.getArea = function() {
return this.width * this.width;
};
var sq = new Square(5);
console.log(sq.getArea()); // 25
var Rectangle = function(width, height) {
this.width = width;
this.height = height;
};
Rectangle.prototype.getArea = function() {
return this.width * this.height;
};
var rect = new Rectangle(3, 4);
console.log(rect.getArea()); // 12
var Square = function(width) {
this.width = width;
this.height = width;
};
Square.prototype.getArea = function() {
return this.width * this.height;
};
var sq = new Square(5);
console.log(sq.getArea()); // 25
var Rectangle = function(width, height) {
this.width = width;
this.height = height;
};
Rectangle.prototype.getArea = function() {
return this.width * this.height;
};
var rect = new Rectangle(3, 4);
console.log(rect.getArea()); // 12
var Square = function(width) {
Rectangle.call(this, width, width);
};
Square.prototype = new Rectangle();
var sq = new Square(5);
console.log(sq.getArea()); // 25
var extendClass1 = function(SuperClass, SubClass, subMethods) {
SubClass.prototype = new SuperClass();
for (var prop in SubClass.prototype) {
if (SubClass.prototype.hasOwnProperty(prop)) {
delete SubClass.prototype[prop];
}
}
if (subMethods) {
for (var method in subMethods) {
SubClass.prototype[method] = subMethods[method];
}
}
Object.freeze(SubClass.prototype);
return SubClass;
};
var Rectangle = function(width, height) {
this.width = width;
this.height = height;
};
Rectangle.prototype.getArea = function() {
return this.width * this.height;
};
var Square = extendClass1(Rectangle, function(width) {
Rectangle.call(this, width, width);
});
var sq = new Square(5);
console.log(sq.getArea()); // 25
var extendClass2 = (function() {
var Bridge = function() {};
return function(SuperClass, SubClass, subMethods) {
Bridge.prototype = SuperClass.prototype;
SubClass.prototype = new Bridge();
if (subMethods) {
for (var method in subMethods) {
SubClass.prototype[method] = subMethods[method];
}
}
Object.freeze(SubClass.prototype);
return SubClass;
};
})();
var Rectangle = function(width, height) {
this.width = width;
this.height = height;
};
Rectangle.prototype.getArea = function() {
return this.width * this.height;
};
var Square = extendClass2(Rectangle, function(width) {
Rectangle.call(this, width, width);
});
var sq = new Square(5);
console.log(sq.getArea()); // 25
var Rectangle = function(width, height) {
this.width = width;
this.height = height;
};
Rectangle.prototype.getArea = function() {
return this.width * this.height;
};
var Square = function(width) {
Rectangle.call(this, width, width);
};
Square.prototype = Object.create(Rectangle.prototype);
Object.freeze(Square.prototype);
var sq = new Square(5);
console.log(sq.getArea()); // 25
var extendClass1 = function(SuperClass, SubClass, subMethods) {
SubClass.prototype = new SuperClass();
for (var prop in SubClass.prototype) {
if (SubClass.prototype.hasOwnProperty(prop)) {
delete SubClass.prototype[prop];
}
}
SubClass.prototype.consturctor = SubClass;
if (subMethods) {
for (var method in subMethods) {
SubClass.prototype[method] = subMethods[method];
}
}
Object.freeze(SubClass.prototype);
return SubClass;
};
var extendClass2 = (function() {
var Bridge = function() {};
return function(SuperClass, SubClass, subMethods) {
Bridge.prototype = SuperClass.prototype;
SubClass.prototype = new Bridge();
SubClass.prototype.consturctor = SubClass;
Bridge.prototype.constructor = SuperClass;
if (subMethods) {
for (var method in subMethods) {
SubClass.prototype[method] = subMethods[method];
}
}
Object.freeze(SubClass.prototype);
return SubClass;
};
})();
var extendClass3 = function(SuperClass, SubClass, subMethods) {
SubClass.prototype = Object.create(SuperClass.prototype);
SubClass.prototype.constructor = SubClass;
if (subMethods) {
for (var method in subMethods) {
SubClass.prototype[method] = subMethods[method];
}
}
Object.freeze(SubClass.prototype);
return SubClass;
};
var extendClass = function(SuperClass, SubClass, subMethods) {
SubClass.prototype = Object.create(SuperClass.prototype);
SubClass.prototype.constructor = SubClass;
SubClass.prototype.super = function(propName) {
// 추가된 부분 시작
var self = this;
if (!propName)
return function() {
SuperClass.apply(self, arguments);
};
var prop = SuperClass.prototype[propName];
if (typeof prop !== 'function') return prop;
return function() {
return prop.apply(self, arguments);
};
}; // 추가된 부분 끝
if (subMethods) {
for (var method in subMethods) {
SubClass.prototype[method] = subMethods[method];
}
}
Object.freeze(SubClass.prototype);
return SubClass;
};
var Rectangle = function(width, height) {
this.width = width;
this.height = height;
};
Rectangle.prototype.getArea = function() {
return this.width * this.height;
};
var Square = extendClass(
Rectangle,
function(width) {
this.super()(width, width); // super 사용 (1)
},
{
getArea: function() {
console.log('size is :', this.super('getArea')()); // super 사용 (2)
},
}
);
var sq = new Square(10);
sq.getArea(); // size is : 100
console.log(sq.super('getArea')()); // 100
var ES5 = function(name) {
this.name = name;
};
ES5.staticMethod = function() {
return this.name + ' staticMethod';
};
ES5.prototype.method = function() {
return this.name + ' method';
};
var es5Instance = new ES5('es5');
console.log(ES5.staticMethod()); // es5 staticMethod
console.log(es5Instance.method()); // es5 method
var ES6 = class {
constructor(name) {
this.name = name;
}
static staticMethod() {
return this.name + ' staticMethod';
}
method() {
return this.name + ' method';
}
};
var es6Instance = new ES6('es6');
console.log(ES6.staticMethod()); // es6 staticMethod
console.log(es6Instance.method()); // es6 method
var Rectangle = class {
constructor(width, height) {
this.width = width;
this.height = height;
}
getArea() {
return this.width * this.height;
}
};
var Square = class extends Rectangle {
constructor(width) {
super(width, width);
}
getArea() {
console.log('size is :', super.getArea());
}
};