ν΄λμ€λ κ³΅ν΅ μμλ₯Ό μ§λλ μ§λ¨μ λΆλ₯νκΈ° μν κ°λ
μ΄λ€.
ES5κΉμ§μ μλ°μ€ν¬λ¦½νΈμλ ν΄λμ€κ° μκ³ , ES6μμ ν΄λμ€κ° λμ
λμλ€.
ν΄λμ€λ νμλ‘ κ°μλ‘ μμ ν΄λμ€μ μμ±μ μμνλ©΄μ λ ꡬ체μ μΈ μκ±΄μ΄ μΆκ° λλ λ³κ²½λλ€. (μμ ν΄λμ€ / νμ ν΄λμ€)
ννΈ, μΈμ€ν΄μ€λ μ΄λ€ ν΄λμ€μ μμ±μ μ§λλ μ€μ‘΄νλ κ°μ²΄
λ₯Ό λ§νλ€.
νλ‘κ·Έλλ° μΈμ΄μμλ ν΄λμ€κ° λ¨Όμ μ μλμ΄μΌλ§ κ·Έλ‘λΆν° 곡ν΅μ μΈ μμλ₯Ό μ§λλ κ°μ²΄(μΈμ€ν΄μ€)λ€μ μμ±ν μ μλ€.
var Rectangle = function (name) {
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
console.log(rect1.isRectangle(rect1)); // Uncaught TypeError: not a function
console.log(Rectangle.isRectangle(rect1)); // true
νλ‘ν νμ
λ©μλλ ν΄λμ€μ prototype λ΄λΆμ μ μλ λ©μλ
λ‘μ μΈμ€ν΄μ€κ° λ§μΉ μμ μ κ²μ²λΌ νΈμΆν μ μλ€.
μ€νν± λ©μλλ ν΄λμ€(μμ±μ ν¨μ)μ μ§μ μ μν λ©μλ
λ‘μ μΈμ€ν΄μ€κ° μ§μ νΈμΆν μ μκ³ ν΄λμ€(μμ±μ ν¨μ)μ μν΄μλ§ νΈμΆν μ μλ€.
ꡬ체μ μΈ μΈμ€ν΄μ€κ° μ¬μ©ν λ©μλλ₯Ό μ μν 'ν'μ μν μ ν λμ ν΄λμ€λ μΆμμ μΈ κ°λ
μ΄λ€.
κ·Έλ¬λ, μ€νν± λ©μλλ₯Ό νΈμΆν λμ ν΄λμ€λ ν΄λμ€(μμ±μ ν¨μ) μμ²΄κ° thisκ° λμ΄ λ©μλμ μ§μ μ κ·Όνλ―λ‘ νλμ ꡬ체μ μΈ κ°μ²΄λ‘μ μ·¨κΈλλ€.
λ€μ€ νλ‘ν νμ
체μΈ
μ μ΄μ©ν΄ ν΄λμ€ μμμ ꡬνν μ μλ€.
μλ°μ€ν¬λ¦½νΈμμ ν΄λμ€ μμμ ꡬννλ€λ κ²μ 곧 νλ‘ν νμ
체μ΄λμ μ μ°κ²°νλ€λ λ»μ΄λ€.
μμ μ°Έκ³ νλ μμ λ₯Ό λ€μ κ°μ Έμλ€.
μμ 1: Array λ΄μ₯ ν΄λμ€λ₯Ό μμνλ νμ ν΄λμ€
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);
console.log(g); // // Grade { 0: 100, 1: 80, length: 2 }
delete g.length; // gμμ length νλ‘νΌν°κ° μμ λλ€
g.push(70); // gμ length κ°μΌλ‘ g.__proto__.length(μ¦, Grade.prototype.length)μ κ°(0)μ μ½μ΄μ¨ ν gμ 70μ push νλ€
console.log(g); // Grade { 0: 70, 1: 80, length: 1 }
κ·Έλ¬λ μ μμ μ½λμλ λͺ κ°μ§ ν° λ¬Έμ κ° μλ€.
λ΄μ₯ κ°μ²΄μΈ λ°°μ΄ μΈμ€ν΄μ€μ length νλ‘νΌν°λ μμ κ° λΆκ°λ₯ν λ°λ©΄, Grade ν΄λμ€μ μΈμ€ν΄μ€λ Grade.prototype = []
μ μν΄ λ°°μ΄ λ©μλλ₯Ό μμνλ©΄μλ, κΈ°λ³Έμ μΌλ‘λ μΌλ° κ°μ²΄μ μ±μ§μ κ·Έλλ‘ μ§λλ―λ‘ length νλ‘νΌν°λ₯Ό μμ ν μ μλ€.
μ΄μ κ°μ μ€λ₯λ₯Ό μ νμ§ μκΈ° μν΄μλ ν΄λμ€μ μλ κ°μ΄ μΈμ€ν΄μ€μ λμμ μν₯μ μ£Όμ§ μκ²λ ν΄λμ€κ° ꡬ체μ μΈ λ°μ΄ν°λ₯Ό μ§λμ§ μλλ‘ ν΄μΌ νλ€.
μμ 2: μ¬μ©μκ° μ μν λ ν΄λμ€ μ¬μ΄μ μμ κ΄κ³
// μ§μ¬κ°ν ν΄λμ€ & μ μ¬κ°ν ν΄λμ€
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(); // νμ ν΄λμ€μ prototypeμ μ°κ²°νκ³ μ νλ μμ ν΄λμ€(μμ±μ ν¨μ)μ μΈμ€ν΄μ€ ν λΉ
var sq = new Square(5);
console.log(sq.getArea()); // 25
console.dir(sq)
μ μ€νν΄ sq μΈμ€ν΄μ€λ₯Ό μ΄ν΄λ³΄λ©΄ ν΄λμ€κ° ꡬ체μ μΈ λ°μ΄ν°λ₯Ό κ°μ§κ³ μμμ νμΈν μ μλ€.
Square
height: 5
width: 5
__proto__: Rectangle // ν΄λμ€κ°
height: undefined // undefined λΌλ κ°μ κ°μ§λ€
width: undefined // undefined λΌλ κ°μ κ°μ§λ€
__proto__ :
getArea: f ()
constructor: f (width, height)
__proto__: Object
λν, sq μΈμ€ν΄μ€μ constructorκ° Squareκ° μλλΌ Rectangleμ μ°Έμ‘°νλ€λ λ¬Έμ μ μ κ°μ§λ€.
var rect2 = sq.constructor(2, 3);
console.log(rect2); // Rectangle { width: 2, height: 3 }
μ 리νμλ©΄, νμ ν΄λμ€λ‘ μΌμ μμ±μ ν¨μμ prototypeμ μμ ν΄λμ€μ μΈμ€ν΄μ€λ₯Ό λΆμ¬ν¨μΌλ‘μ¨(λ€μ€ νλ‘ν νμ 체μΈ) κΈ°λ³Έμ μΈ λ©μλ μμμ κ°λ₯νμ§λ§ λ€μν λ¬Έμ κ° λ°μν μ¬μ§κ° μλ€κ³ ν μ μλ€.
ν΄λμ€ μμ λ° μΆμνλ₯Ό μν΄μλ
SubClass.prototype.__proto__
κ°SuperClass.prototype
μ μ°Έμ‘°νκ³ ,SubClass.prototype
μλ λΆνμν μΈμ€ν΄μ€ νλ‘νΌν°(ꡬ체μ μΈ λ°μ΄ν°)κ° λ¨μ§ μλλ‘ ν΄μΌ νλ€.
λ°©λ² 1: μΈμ€ν΄μ€ μμ± ν νλ‘νΌν° μ κ±°
μμ ν΄λμ€μ μΈμ€ν΄μ€(νμ ν΄λμ€μ prototype κ°μ²΄)λ₯Ό λ§λ ν νμ ν΄λμ€κ° ꡬ체μ μΈ λ°μ΄ν°λ₯Ό μ§λμ§ μλλ‘ κ·Έ νλ‘νΌν°λ₯Ό μ κ±°νλ€.
μ΄νλ‘ λ³κ²½ νΉμ μμ ν μ μλλ‘ Object.freeze()λ₯Ό μ¬μ©νλ€.
delete Square.prototype.width;
delete Square.prototype.height;
Object.freeze(Square.prototype);
λ°©λ² 2: λΉ ν¨μ νμ©
var Bridge = function () {}; // λΉ μμ±μ ν¨μ
Bridge.prototype = Rectangle.prototype;
Square.prototype = new Bridge();
Object.freeze(Square.prototype);
λ°©λ² 3: Object.create() νμ©
νμ ν΄λμ€μ prototypeμ __proto__
κ°, μμ ν΄λμ€μ prototypeμ μ°Έμ‘°νλ, (νμ ν΄λμ€κ° ꡬ체μ μΈ λ°μ΄ν°λ₯Ό μ§λμ§ μλλ‘) μμ ν΄λμ€μ μΈμ€ν΄μ€κ° λμ§λ μλλ‘ νλ€.
...
Square.prototype = Object.create(Rectangle.prototype);
Object.freeze(Square.prototype);
...
μμμ μ΄ν΄λ΄€λ―μ΄ SubClass.prototype.constructorκ° SuperClassλ₯Ό κ°μ§κ³ μμ΄μ SubClass.constructorλ₯Ό μ€ννλ©΄ μλμ λ€λ₯Έ κ²°κ³Όκ° λμ¨λ€.
ν΄λμ€ μμ λ° μΆμνλ₯Ό μν΄ μΆκ°μ μΌλ‘ SubClass.prototype.constructorκ° SubClassλ₯Ό μ°Έμ‘°νλλ‘ ν΄μΌ νλ€.
λ²μ©μ±μ κ³ λ €ν μ½λμ μλ μ½λλ₯Ό μΆκ°νλλ‘ νλ€.
SubClass.prototype.constructor = SubClass;
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());
}
};
μμ ν΄λμ€λ₯Ό μμλ°λ νμ ν΄λμ€λ₯Ό λ§λ€κΈ° μν΄ class λͺ
λ Ήμ΄ λ€μ 'extends μμ ν΄λμ€ μ΄λ¦'
μ μΆκ°νλ€.
constructor λ©μλ λ΄λΆ
μμ super ν€μλλ₯Ό 'ν¨μ'μ²λΌ μ¬μ©ν μ μλλ°, μ΄λ μμ ν΄λμ€μ constructor(μμ±μ ν¨μ)λ₯Ό μ€ννλ€.
constructor λ©μλλ₯Ό μ μΈν λ€λ₯Έ λ©μλ
μμλ super ν€μλλ₯Ό 'κ°μ²΄'μ²λΌ μ¬μ©ν μ μλ€.
μ΄λ κ°μ²΄λ μμ ν΄λμ€μ prototype κ°μ²΄λ₯Ό μ°Έμ‘°νλ€.
κ·Έλ¬λ, νΈμΆν λ©μλ λ΄λΆμμμ thisλ μλμ this(μ¦, νμ ν΄λμ€μ prototype κ°μ²΄)λ₯Ό κ·Έλλ‘ λ°λ₯Έλ€.