โœŠ ํ•„์˜ค์˜ ๊ฐœ๋ฐœ์ผ์ง€
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

ยฉ 2026 Felix