✊ ν•„μ˜€μ˜ κ°œλ°œμΌμ§€
Back to Posts
2018λ…„ 10μ›” 20일

6/ OOP (ES5 κΈ°μ€€)

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()); // Joy
const you = new Person('Gray'); const him = new Person('Lee');
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. 상속 - μžλ°”μŠ€ν¬λ¦½νŠΈλŠ” 클래슀λ₯Ό 기반으둜 ν•˜λŠ” 전톡적인 상속을 μ§€μ›ν•˜μ§€ μ•ŠλŠ”λ‹€. - μžλ°”μŠ€ν¬λ¦½νŠΈ

νŠΉμ„± 쀑 객체 ν”„λ‘œν† νƒ€μž… 체인을 μ΄μš©ν•˜μ—¬ 상속을 κ΅¬ν˜„ν•΄λ‚Ό 수 μžˆλ‹€.

상속 κ΅¬ν˜„λ°©λ²•

  1. 클래슀 기반 전톡적인 상속 방식을 흉내냄 (μ»¨ν…μŠ€νŠΈ 자체λ₯Ό μƒμ†λ°›μŒ)
  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
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 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());
  1. person객체λ₯Ό κ°–κ³ μžˆλŠ” ν”„λ‘œν† νƒ€μž…μ„ κ°–κ³ μžˆλŠ” student μΈμŠ€ν„΄μŠ€(μ»¨ν…μŠ€νŠΈ)κ°€ addedλ₯Ό μƒμ†λ°›λŠ”λ‹€. (deep copy함)
  2. 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());
  1. youλŠ” Person의 μΈμŠ€ν„΄μŠ€ (name: JoyKim)
  2. Student의 ν”„λ‘œν† νƒ€μž…μ€ youλ₯Ό 가리킨닀.
  3. meλŠ” Student의 μΈμŠ€ν„΄μŠ€
  1. 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');
  1. Function ν”„λ‘œν† νƒ€μž…μ— ν”„λ‘œν† νƒ€μž… ν•¨μˆ˜λ₯Ό λ§Œλ“€μ–΄μ£ΌλŠ”, μž¬μ‚¬μš©ν•  수 μžˆλŠ” methodλΌλŠ” λ©”μ„œλ“œλ₯Ό μΆ”κ°€ν•œλ‹€. (μ•„μ˜€ 넀이밍 μ˜ˆμ‹œ ν—·κ°ˆ)
  2. F의 ν”„λ‘œν† νƒ€μž…μ„ Person의 ν”„λ‘œν† νƒ€μž…μ„ μ°Έμ‘°ν•˜κ²Œ ν•¨μœΌλ‘œμ¨ 쀑간역할을 ν•˜κ²Œν•¨
  3. κ·Έ 쀑간역할을 ν•˜λŠ” F의 μΈμŠ€ν„΄μŠ€λ₯Ό Student의 ν”„λ‘œν† νƒ€μž…μ΄ μ°Έμ‘°ν•˜λ„λ‘ 함. (ν˜„μž¬ Student => F => Person)
  1. 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);
// λͺ¨λ“ˆνŒ¨ν„΄ 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())


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'); // false

4-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 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.

Previous1/ ν•¨μˆ˜ν˜• ν”„λ‘œκ·Έλž˜λ° (πŸ™„)
NextCodeSpitz78 5/ OOAD와 ν…ŒνŠΈλ¦¬μŠ€ (2)

Related

Β© 2025 Felix