6/ OOP (ES5 κΈ°μ€)
π μΈμ¬μ΄λ μλ°μ€ν¬λ¦½νΈ μ€ λ©λͺ¨ν΄μΌν λΆλΆλ§ μ μμ΅λλ€.
0. ν΄λμ€ κΈ°λ°μ μΈμ΄ - νλ‘ν νμ κΈ°λ°μ μΈμ΄ #### ν΄λμ€ κΈ°λ°μ μΈμ΄ - ν΄λμ€λ‘ κ°μ²΄μ κΈ°λ³Έμ μΈ
ννμ κΈ°λ₯μ μ μνκ³ , μμ±μλ‘ μΈμ€ν΄μ€λ₯Ό λ§λ€μ΄μ μ¬μ©ν μ μλ€. - λ°νμμ λ°κΏ μ μλ€. - μ νμ±, μμ μ±, μμΈ‘μ±λ±μ κ΄μ μμλ νλ‘ν νμ κΈ°λ°μ μΈμ΄λ³΄λ€ μ’λ λμ κ²°κ³Όλ₯Ό 보μ₯. - JAVA, C++
νλ‘ν νμ κΈ°λ°μ μΈμ΄
- κ°μ²΄μ μλ£κ΅¬μ‘°, λ©μλ λ±μ λμ μΌλ‘ λ°κΏ μ μλ€.
- μλ°μ€ν¬λ¦½νΈ
1. ν΄λμ€, μμ±μ, λ©μλ
function Person(arg) {
// ν΄λμ€μ΄μ, μμ±μμ μν μ ν¨.
this.name = arg;
this.getName = function () {
return this.name;
};
this.setName = function (value): void {
this.name = value;
};
}
const me = new Person('Kim');
console.log(me.getName()); // Kim
me.setName('Joy');
console.log(me.getName()); // Joyconst you = new Person('Gray');
const him = new Person('Lee');- 곡ν΅μ μΌλ‘ μ¬μ©λ μ μλ setNameκ³Ό getName λ©μλκ° μ€λ³΅μΌλ‘ λ©λͺ¨λ¦¬μ μ¬λ €λκ² λλ€.
- setNameκ³Ό getNameμ μ¬μ¬μ©νμ!
function Person(arg) {
this.name = arg;
}
Person.prototype.getName = function () {
return this.name;
};
Person.prototype.setName = function (value) {
this.name = value;
};
const you = new Person('Gray');
const him = new Person('Lee');
console.log(you.getName()); // thisλ μμ μ νΈμΆν κ°μ²΄μ λ°μΈλ©λλ€.
console.log(him.getName()); // νλ‘ν νμ
체μΈμΌλ‘ μ κ·Όν μ μλ€.νλ‘ν νμ λ©μλλ₯Ό λ§λλ 루ν΄μ ν¨μ체μΈμ λ μμμΈ Function νλ‘ν νμ μ methodλΌλ μ΄λ¦μΌλ‘ λ§λ€μ΄λκ³ μ¬μ¬μ©νλ λ°©λ²λ μλ€.
Function.prototype.method = function(name, function) {
if(!this.prototpye[name]) {
this.prototype[name] = function;
}// νλ‘ν νμ
μ κ°μ μ΄λ¦μ λ©μλκ° μλ€λ©΄
}
function Person(arg) {
this.name = arg;
}
Person.method('setName', function(value){
this.name = value;
})
Person.method('getName', function() {
return this.name;
})
const me = new Person("me");
const you = new Person("you");
console.log(me.getName());
console.log(you.getName());2. μμ - μλ°μ€ν¬λ¦½νΈλ ν΄λμ€λ₯Ό κΈ°λ°μΌλ‘ νλ μ ν΅μ μΈ μμμ μ§μνμ§ μλλ€. - μλ°μ€ν¬λ¦½νΈ
νΉμ± μ€ κ°μ²΄ νλ‘ν νμ 체μΈμ μ΄μ©νμ¬ μμμ ꡬνν΄λΌ μ μλ€.
μμ ꡬνλ°©λ²
- ν΄λμ€ κΈ°λ° μ ν΅μ μΈ μμ λ°©μμ νλ΄λ (컨ν μ€νΈ μ체λ₯Ό μμλ°μ)
- ν΄λμ€ κ°λ μμ΄ κ°μ²΄μ νλ‘ν νμ μΌλ‘ μμμ ꡬννλ λ°©μ => Prototypeal inheritance
2-1. νλ‘ν νμ μ μ΄μ©ν μμ
function create_object(o) {
function F() {}
F.prototype = o;
return new F();
} // Object.create() ν¨μλ‘ μ 곡λλ€.μΈμλ‘ λ€μ΄μ¨ κ°μ²΄(o)λ₯Ό λΆλͺ¨λ‘ νλ μμ κ°μ²΄(F)λ₯Ό μμ±νμ¬ λ°ννλ€. => νλ‘ν νμ μ νΉμ±μ νμ©νμ¬ μμμ ꡬννλκ² = νλ‘ν νμ κΈ°λ°μ μμ
μμ
function create_object(o) {
function F() {}
F.prototype = o;
return new F();
}
var person = {
name: "Joy"
getName: function() { // ES6μ getter κ°λ
return this.name;
}
setName: function(arg){ // ES6μ setter κ°λ
this.name = arg;
}
}
var me = create_object(person)
me.name // Joy
me.getName() // Joy
me.setName("Kim")
me.name // Kim
me.getName() // Kim- ν΄λμ€μ ν΄λΉνλ μμ±μ ν¨μλ₯Ό λ§λ€μ§λ μμκ³ ,
- κ·Έ ν΄λμ€μ μΈμ€ν΄μ€λ₯Ό λ°λ‘ μμ±νμ§λ μμλ€.
- person κ°μ²΄μ μ΄ κ°μ²΄λ₯Ό νλ‘ν νμ 체μΈμΌλ‘ μ°Έμ‘°ν μ μλ μμ κ°μ²΄ meλ₯Ό λ§λ€μ΄μ μ¬μ©ν¨.
me.setAge = function(age) {...}
me.getAge = function() {...}μμ λ°©μμΌλ‘ νμ₯ν μ μμ§λ§, μ½λκ° μ§μ λΆν΄μ§ μ μλ€. **extend()**λΌλ μ΄λ¦μ ν¨μλ‘ κ°μ²΄μ μμ μ΄ μνλ κ°μ²΄ νΉμ ν¨μλ₯Ό μΆκ°μν¨λ€.
__jQueryμ extend ν¨μ
jQuery.extend = jQuery.fn.extend = function (obj: μμ, prop: λΆλͺ¨) {
if (!prop) {
prop = obj; // λΆλͺ¨κ° μμΌλ©΄ μμμ΄ λΆλͺ¨
obj = this; // thisλ₯Ό μμμκ² ν λΉ
}
for (var i in prop) {
// deep copy
ob[i] = prop[i];
}
return obj;
};jQuery.fnμ jQueryμ νλ‘ν νμ- $.extend()λ
var elem = new jQuery(...); elem.extend()ννλ‘ νΈμΆκ°λ₯ ob[i] = prop[i];μ μμ λ³΅μ¬ (shallow copy) => μ°Έμ‘°κ°μ 볡μ¬νλ κ²½μ° μν₯μ΄ μκΈ΄λ€. μ΄λ₯Ό λ°©μ§νκΈ° μν΄ κΉμ 볡μ¬λ₯Ό ν΄μΌν¨.- κΉμ 볡μ¬λ₯Ό μν΄μ λΉ κ°μ²΄λ₯Ό λ§λ€μ΄μ extend ν¨μλ₯Ό μ¬κ·μ μΌλ‘ νΈμΆ
// jQuery extend ν¨μ μ€ μΌλΆ
...
for (; i < length; i++){
if( (options = arguments[i]) != null) {
/* μΈμλ‘ λμ΄μ¨ κ°μ²΄μ νλ‘νΌν°λ₯Ό optionsλ‘ μ°Έμ‘°μν€κ³ ,
κ·Έ νλ‘νΌν°κ° nullμ΄ μλ κ²½μ° λΈλ‘ μμΌλ‘ μ§μ
νλ€. */
for (name in options){ // optionsλ₯Ό deep μΉ΄νΌνλ€.
src = target[name]; // srcλ λ°νλ 볡μ¬λ³Έ targetμ κ°λ¦¬ν΄
copy = options[name]; // copyλ 볡μ¬ν μλ³Έ νλ‘νΌν°λ₯Ό κ°λ¦¬ν΄
if (target === copy){ // 무ν루ν λ°©μ§
continue; // continueλ 루νμ μ€νμ μμ ν μ’
λ£νμ§ μκ³ for 루νμμλ μ
λ°μ΄νΈ ννμμΌλ‘ μ νν¨.
}
if (deep && copy && (jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy))) ){
// deep νλκ·Έ:boolean: extendμμ μΈμλ‘ λ°μ : κΉμ 볡μ¬λ₯Ό ν κ²μΈμ§ μ ν ν μ μκ² νλ€.
// copy: μ°Έμ‘°νμμΈ κ²½μ° (κ°μ²΄λ λ°°μ΄μΈ κ²½μ°) 무쑰건 deep copy μμ
)
if ( copyIsArray ) {
copyIsArray = false;
clone = src && jQuery.isArray(src) ? src : [];
// λ°°μ΄ λ³΅μ¬μΌ κ²½μ° λΉ λ°°μ΄ μμ±
} else {
clone = src && jQuery.isPlainObject(src) ? src : {};
// κ°μ²΄ 볡μ¬μΌ κ²½μ° λΉ κ°μ²΄ μμ±
}
target[name] = jQuery.extend(deep, clone, copy); // μ¬κ·..
} else if (copy !== undefined) {
target[name] = copy;
}
}
}
return target;
}const person = {
name: 'joy',
getName: function(){ return this.name; },
setName: function(arg){this.name = arg;}
}
function create_object(o) {
function F() {};
F.prototype = o;
return new F();
}
function extend(obj, prop) {
if(!prop) {prop = obj; obj = this;}
/* μΈμκ° νλλ§ λ€μ΄μ€λ κ²½μ°,
prop μΈμμ objλ₯Ό ν λΉνκ³ ,
νμ¬κ°μ²΄(this)μ κ°μ²΄μ νλ‘νΌν°λ₯Ό 볡μ¬νλ€.... */
for (let i in prop) obj[i] = prop[i]; // μμ 볡μ¬μ.
return obj;
}
const student = create_object(person);
const added = {
setAge: function(age){ this.age = age; }
getAge: function() {return this.age;}
}
extend(student, added); // 1
student.setAge(25);
console.log(student.getAge());- personκ°μ²΄λ₯Ό κ°κ³ μλ νλ‘ν νμ μ κ°κ³ μλ student μΈμ€ν΄μ€(컨ν μ€νΈ)κ° addedλ₯Ό μμλ°λλ€. (deep copyν¨)
- student μΈμ€ν΄μ€μλ added κ°μ²΄κ° 볡μ¬λλ€.
2-2. ν΄λμ€ κΈ°λ°μ μμ
1λ²μ κ°μ²΄μ μμμ΄μκ³ , μ§κΈμ ν΄λμ€μ μν μ νλ ν¨μλ₯Ό μμνλ κ²μ μ€λͺ νλ€. (컨ν μ€νΈ μμ)
function Person(arg) { this.name = arg;}
Person.prototype.setName = function(value) { this.name = value; };
Person.prototype.getName = function() { return this.name; }
function Student(arg) {}
const you = new Person('JoyKim'); Student.prototype = you;
const me = new Student('NaYoung'); me.setName('Kim'); console.log(me.getName());
- youλ Personμ μΈμ€ν΄μ€ (name: JoyKim)
- Studentμ νλ‘ν νμ μ youλ₯Ό κ°λ¦¬ν¨λ€.
- meλ Studentμ μΈμ€ν΄μ€
- Studentμλ μΈμλ₯Ό λ°μ nameμ΄ μμΌλ―λ‘ βNaYoungβμ λ£μ΄μ€λ μλ¬΄λ° μ μ© μλ¨
- μ΄λ₯Ό μν΄μ μΈμ€ν΄μ€κ° μμ±λ λ λΆλͺ¨ν¨μκ° λ°λ‘ μ€νλ μ μλλ‘ Studentμ μ€νμ½λλ₯Ό λ£λλ€.
function Student(arg){ Person.apply(this, arg) // λΆλͺ¨ν¨μ Pseronμ μ€ννκ³ thisλ argμ λ°μΈλ©! }
- meκ°μ²΄μμ setNameμ νΈμΆνλ©΄ νλ‘ν νμ 체μ΄λμ μν΄μ PersonκΉμ§μ¬λΌκ°λ€.
μ΄ λ‘μ§μ λ¨μ μ meμ prototypeμ΄ Student.prototypeμ΄κ³ , μ΄λ κ³³ youλ₯Ό κ°λ¦¬ν¨λ€λ κ²μΈλ°, μ΄λ κ² λλ©΄ meκ° youμ μμ κ°λ μ΄ λλ©΄μ μλͺ»λ μ€κ³κ° λλ€.
meμ youμ λ 립μ±μ μν΄μ μ€κ° μν μ ν΄μ£Όλ νλ‘ν μ½ λΉ ν¨μ F()λ₯Ό μΆκ°νλ€.
function Person(arg) { this.name = arg;}
Function.prototype.method = function(name, func) { this.prototype[name] = func };
Person.method('setName', function(value) { this.name = value; };)
Person.method('getName', function() { return this.name; })
function Student(arg) {}
function F(){};
F.prototype = Person.prototype;
Student.prototype = new F();
Student.prototype.constructor = Student;
Student.super = Person.prototype;
const me = new Student('NaYoung');
const you = new Person('YoungRan');
me.setName('Kim');- Function νλ‘ν νμ μ νλ‘ν νμ ν¨μλ₯Ό λ§λ€μ΄μ£Όλ, μ¬μ¬μ©ν μ μλ methodλΌλ λ©μλλ₯Ό μΆκ°νλ€. (μμ€ λ€μ΄λ° μμ ν·κ°)
- Fμ νλ‘ν νμ μ Personμ νλ‘ν νμ μ μ°Έμ‘°νκ² ν¨μΌλ‘μ¨ μ€κ°μν μ νκ²ν¨
- κ·Έ μ€κ°μν μ νλ Fμ μΈμ€ν΄μ€λ₯Ό Studentμ νλ‘ν νμ μ΄ μ°Έμ‘°νλλ‘ ν¨. (νμ¬ Student => F => Person)
- νλ‘ν νμ 체μΈμ μν΄μ Student νλ‘ν νμ μ μμ±μλ₯Ό Studentλ‘ ν λΉ.
- Studentκ° Person.prototypeμλ μ κ·ΌνκΈ° μν΄μ superλΌλ λ©μλ μμ±
meλ Personμ μμλ°μ Studentμ μΈμ€ν΄μ€μ΄κ³ youλ Personμ μΈμ€ν΄μ€.
μ λ‘μ§μ λͺ¨λν μν€λ©΄..(by.μ€ν μ μ€ν νλ Έν[JavaScript Pattersn])
const inherit = function (Parent, Child) {
const F = function () {};
return function (Paretn, Child) {
F.prototype = Parent.prototype;
Child.prototype = new F();
Child.prototype.constructor = Child;
Child.super = Parent.prototype;
};
};ν΄λ‘μ λ F()ν¨μλ₯Ό μ§μμ μΌλ‘ μ°Έμ‘°νλ€. F()λ κ°λΉμ§ 컬λ μ μ λμμ΄ λμ§ μκ³ κ³μ λ¨μ μλ€. μ΄λ₯Ό μ΄μ©ν΄ ν¨μ F()μ μμ±μ λ¨ ν λ² μ΄λ£¨μ΄μ§κ³ inheritν¨μκ° κ³μν΄μ νΈμΆλμ΄λ ν¨μ F()μ μμ±μ μλ‘ ν νμκ° μλ€.
3. μΊ‘μν (feat. ν΄λ‘μ ) - μ 보 곡κ°μ μ¬λΆ. μ 보 μλ κ°λ
(Typescriptμμλ public,
private, protected λ©€λ²λ₯Ό μ μΈν¨μΌλ‘μ¨ ν΄λΉ μ 보λ₯Ό μΈλΆλ‘ λ
ΈμΆμν¬μ§ μ¬λΆλ₯Ό κ²°μ ) - μλ°μ€ν¬λ¦½νΈ
es6μμλ get, set ν€μλλ‘ μΈλΆμμ ν΄λΉ ν΄λμ€ νΉμ ν¨μ λ΄λΆμ μ κ·Ό ν μ μλ€. (getμ readonly)
const Person = function (arg) {
let name = arg ? arg : 'joy';
this.getName = function () {
return name;
};
this.setName = function (arg) {
name = arg;
};
};
const me = new Person();
console.log(me.getName());
me.setName('NaYoung');
console.log(me.getName());
console.log(me.name);- Personμ λ΄λΆ public ν¨μλ€μ **ν΄λ‘μ **μν μ νλ©΄μ nameμ μ κ·Όνκ³ μλ€.
// λͺ¨λν¨ν΄
const Person = function(arg){
const name = arg? arg: 'joy';
// nameμ private λ©€λ²
return {
getName: function() { return name;},
setName: fucntion(arg){ name = arg;}
}
}
const me = Person()
console.log(me.getName())- μ κ·Όνλ private λ©€λ²κ° κ°μ²΄λ λ°°μ΄μ΄λ©΄(λ νΌλ°μ€) μμ 볡μ¬λ‘ μ°Έμ‘°λ§μ λ°ννλ―λ‘ μ¬μ©μκ° μ΄ν μ΄λ₯Ό μ½κ² λ³κ²½ν μ μλ€. (Deep copy, Shallow copy)
- κ°μ²΄λ§μ λ°ννκΈ° λλ¬Έμ Person ν¨μμ νλ‘ν νμ
μ μ κ·Όν μ μλ€.
const Person3 = (function (arg) { const name = arg ? arg : 'joy'; var Func = function () {}; Func.prototype = { getName: function () { return name; }, setName: function (arg) { name = arg; }, }; return Func; })();
4. κ°μ²΄μ§ν₯ νλ‘κ·Έλλ° μμ© μμ
ν¨μμ νλ‘ν νμ μ²΄μΈ extend ν¨μ μΈμ€ν΄μ€λ₯Ό μμ±ν λ μμ±μ νΈμΆμ μ΄μ©ν΄μ μλ°μ€ν¬λ¦½νΈλ‘ ν΄λμ€ κΈ°λ₯μ νλ ν¨μ λ§λ€κΈ°
4-1-1. subClass ν¨μ ꡬ쑰
subClassν¨μλ λ³μ λ° λ©μλκ° λ΄κΈ΄ κ°μ²΄λ₯Ό μΈμλ‘ λ°μ λΆλͺ¨ ν¨μλ₯Ό μμλ°λ μμ ν΄λμ€λ₯Ό λ§λ λ€.
λΆλͺ¨ν¨μλ subClass() ν¨μλ₯Ό νΈμΆν λ this κ°μ²΄λ₯Ό μλ―Ένλ€.
const SuperClass = subClass(obj); // μμλ°μ ν΄λμ€
const SubClass = SuperClass.subClass(obj); // SubClassλ SuperClassλ₯Ό μμλ°λλ€.μ΄μ²λΌ SuperClassλ₯Ό μμλ°λ subClassλ₯Ό λ§λ€κ³ μ ν λ, SuperClass.subClass()μ νμμΌλ‘ νΈμΆνκ² κ΅¬ννλ€. μ°Έκ³ λ‘ μ΅μμ ν΄λμ€μΈ SuperClassλ μλ°μ€ν¬λ¦½νΈμ Fucntionμ μμλ°κ² νλ€.
function subClass(obj) {
/*
1. μμ ν΄λμ€ (ν¨μκ°μ²΄) μμ±
2. μμ±μ νΈμΆ (ν΄λμ€ ν¨μλ₯Ό μμ±νκΈ° μν΄μ)
3. νλ‘ν νμ
체μΈμ νμ©ν μμ ꡬν
4. objλ₯Ό ν΅ν΄ λ€μ΄μ¨ λ³μ λ° λ©μλλ₯Ό μμ ν΄λμ€μ μΆκ°
5. μμ ν¨μ κ°μ²΄ λ°ν
*/
}4-1-2. μμ ν΄λμ€ μμ± λ° μμ
function subClass(obj) {
...
const parent = this; // λΆλͺ¨ν΄λμ€λ₯Ό κ°λ¦¬ν€λ parentλ thisλ₯Ό κ·Έλλ‘ μ°Έμ‘°
const F = function(){}; // μ€κ°μν
const child = function(){}; // μμκ°μ²΄
F.prototype = parent.prototype;
child.prototype = new F(); // λΆλͺ¨μ νλ‘ν νμ
μ μ°Έμ‘°νλ νλ‘ν νμ
μ κ°κ³ μλ Fλ‘λΆν° λ§λ€μ΄μ§ μμ±μ ν¨μλ₯Ό child νλ‘ν νμ
μ΄ μ°Έμ‘°νλλ‘νλ€.γ
child.prototype.constructor = child;
child.parent = parent.prototype;
child.parent_constructor = parent;
...
return child;
}μμ ν΄λμ€λ child λΌλ μ΄λ¦μ ν¨μ κ°μ²΄λ₯Ό μμ±ν¨μΌλ‘μ¨ λ§λ€μ΄μ‘λ€.
4-1-3. μμ ν΄λμ€ νμ₯
for (let i in obj) {
if (obj.hasOwnProperty(i)) {
child.prototype[i] = obj[i];
}
}hasOwnProperty μΈμλ‘ λκΈ°λ μ΄λ¦μ ν΄λΉνλ νλ‘νΌν°κ° κ°μ²΄ λ΄μ μλμ§λ₯Ό νλ€.
νλ‘ν νμ 체μΈμ νκ³ μ¬λΌκ°μ§ μκ³ ν΄λΉκ°μ²΄ λ΄μμλ§ μ°Ύλλ€λ κ²μ μ μ
o = new Object();
o.prop = 'exists';
o.hasOwnProperty('prop'); // true
o.hasOwnProperty('toString'); // false
o.hasOwnProperty('hasOwnProperty'); // false4-1-4. μμ±μ νΈμΆ
ν΄λμ€μ μΈμ€ν΄μ€κ° μμ±λ λ, ν΄λμ€ λ΄μ μ μλ μμ±μκ° νΈμΆλΌμΌνλ€. λΆλͺ¨ ν΄λμ€μ μμ±μ μμ νΈμΆλμ΄μΌνλ€. (μ΄κΈ°νλ₯Ό μν΄μ)
const child = function () {
if (parent.hasOwnProperty('_init')) {
parent._init.apply(this, arguments);
}
if (child.prototype.hasOwnProperty('_init')) {
child.prototype._init.apply(this, arguments);
}
};const SuperClass = subClass();
const SubClass = SuperClass.subClass();
const Sub_SubClass = SubClass.subClass();
const instance = new Sub_SubClass();instance μμ±μ SuperClass μμ±μκ° νΈμΆλμ§ μλλ€. => λΆλͺ¨ν΄λμ€μ μμ±μλ₯Ό νΈμΆνλ μ½λλ₯Ό μ¬κ·μ μΌλ‘ ꡬννμ¬ ν΄κ²°νλ€.
const child = function () {
const _parent = child.parent_constructor;
if (_parent && _parent !== Function) {
_parent.apply(this, arguments);
}
if (child.prototype.hasOwnProperty('_init')) {
child.prototype._init.apply(this, arguments);
}
};- νμ¬ ν΄λμ€μ λΆλͺ¨ μμ±μκ° μμΌλ©΄, κ·Έ ν¨μλ₯Ό νΈμΆνλ€. λ€λ§ λΆλͺ¨κ° Functionμ΄ κ²½μ°λ μ΅μμ ν΄λμ€μ λλ¬νμΌλ―λ‘ μ€ννμ§ μλλ€.
μ΅μ’
function subClass(obj){
/*
1. μμ ν΄λμ€ (ν¨μκ°μ²΄) μμ±
2. μμ±μ νΈμΆ (ν΄λμ€ ν¨μλ₯Ό μμ±νκΈ° μν΄μ)
3. νλ‘ν νμ
체μΈμ νμ©ν μμ ꡬν
4. objλ₯Ό ν΅ν΄ λ€μ΄μ¨ λ³μ λ° λ©μλλ₯Ό μμ ν΄λμ€μ μΆκ°
5. μμ ν¨μ κ°μ²΄ λ°ν
*/
const parent = this === window ? Function : this;
const F = function(){}
const child = function() {
const _parent = child.parent;
if(_parent && _parent !== Function){
parent.apply(this, arguments);
}
if(child.prototype._init)){
child.prototype._init.apply(this, arguments);
}
}
F.prototype = parent.prototype;
child.prototype = new F();
child.prototype.constructor = child;
child.parent = parent;
child.subClass = arguments.callee;
for (let i in obj){
if (obj.hasOwnProperty(i)){
child.prototype[i] = obj[i];
}
}
return child
}By Joy.
- λ΄λΆν¨μλ₯Ό μ μΈν λμ thisλ‘ λ°μΈλ©λ ν¨μλ₯Ό μ μΈνλ κ²μ μ°¨μ΄μ ? κΈ°μ€?
- νλ‘ν νμ 체μ΄λμ λ§λ μ΄μ κ°μ²΄μ§ν₯ νλ‘κ·Έλλ°μ μ§μνκΈ° μν΄ λΆλͺ¨κ°μ²΄λ₯Ό κ°λ¦¬ν€λ μ°Έμ‘°λ§ν¬ ννλ‘ μ¨κ²¨μ§ νλ‘νΌν°.
- (Naming) 볡μ¬μ 볡μ¬ν λμμ copy, 볡μ¬μ κ²°κ³Όλ¬Όμ clone
- shallow copyμ λ€λ₯΄κ² deep copyλ μ¬κ·μ μΌλ‘ νΈμΆν΄μΌν¨. (λ€μ μ 리)