๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

JavaScript ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ

ยท ์•ฝ 7๋ถ„

JavaScript ๊ฐ์ฒด ๊ธฐ๋ณธ#

๊ฐ์ฒด๋Š” ๊ด€๋ จ๋œ ๋ฐ์ดํ„ฐ์™€ ํ•จ์ˆ˜์˜ ์ง‘ํ•ฉ์ด๋‹ค. (ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์†Œ๋“œ)

๊ฐ์ฒด๋Š” ๊ณ ์œ ์˜ ํŒจํ‚ค์ง€์— ์ •๋ณด๋ฅผ ์•ˆ์ „ํ•˜๊ฒŒ ๋ณดํ˜ธํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด ๋ฐฉ์‹#

var person = {    name: ['Bob', 'Smith'],    age: 32,    gender: 'male',    interests: ['music', 'skiing'],    bio: function () {        alert(            this.name[0] +                ' ' +                this.name[1] +                ' is ' +                this.age +                ' years old. He likes ' +                this.interests[0] +                ' and ' +                this.interests[1] +                '.',        );    },    greeting: function () {        alert("Hi! I'm " + this.name[0] + '.');    },};

๊ฐ์ฒด์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” ์  ํ‘œ๊ธฐ๋ฒ•(person.name)๊ณผ ๊ด„ํ˜ธ ํ‘œ๊ธฐ๋ฒ•(person['age']) ์ด ์žˆ๋‹ค.

this๋Š” ๋ฌด์—‡์ธ๊ฐ€?#

this ํ‚ค์›Œ๋“œ๋Š” ์ง€๊ธˆ ๋™์ž‘ํ•˜๊ณ  ์žˆ๋Š” ์ฝ”๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

Java์—์„œ์˜ this๋Š” ์ธ์Šคํ„ด์Šค ์ž์‹ (self)๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ฐธ์กฐ๋ณ€์ˆ˜์ด๋‹ค. ์ฃผ๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜์™€๊ฐ์ฒด ์ž์‹ ์ด ๊ฐ–๊ณ  ์žˆ๋Š” ๋ฉค๋ฒ„๋ณ€์ˆ˜๋ช…์ด ๊ฐ™์„ ๊ฒฝ์šฐ ์ด๋ฅผ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋œ๋‹ค.

public Class Person {
  private String name;
  public Person(String name) {    this.name = name;  }}

ํ•˜์ง€๋งŒ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ฒฝ์šฐ Java์™€ ๊ฐ™์ด this์— ๋ฐ”์ธ๋”ฉ๋˜๋Š” ๊ฐ์ฒด๋Š” ํ•œ๊ฐ€์ง€๊ฐ€ ์•„๋‹ˆ๋ผ, ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฉ์‹์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง„๋‹ค.

  1. ํ•จ์ˆ˜ ํ˜ธ์ถœ : ๊ธฐ๋ณธ์ ์œผ๋กœ this๋Š” ์ „์—ญ๊ฐ์ฒด์— ๋ฐ”์ธ๋”ฉ ๋œ๋‹ค. ์‹ฌ์ง€์–ด ๋‚ด๋ถ€ํ•จ์ˆ˜, ๋ฉ”์†Œ๋“œ์˜ ๋‚ด๋ถ€ํ•จ์ˆ˜, ์ฝœ๋ฐฑํ•จ์ˆ˜์˜ ๊ฒฝ์šฐ๋„ ์ „์—ญ๊ฐ์ฒด์— ๋ฐ”์ธ๋”ฉ๋œ๋‹ค. 1-1. apply/call/bind ํ˜ธ์ถœ : ์›ํ•˜๋Š” this์— ๋ฐ”์ธ๋”ฉํ•˜์—ฌ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
  2. ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ : ๋ฉ”์†Œ๋“œ๋ฅผ ์†Œ์œ ํ•œ ๊ฐ์ฒด, ํ•ด๋‹น ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด์— ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.
  3. ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ํ˜ธ์ถœ :
    1. ๋นˆ ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐ this ๋ฐ”์ธ๋”ฉ
    2. this๋ฅผ ํ†ตํ•œ ํ”„๋กœํผํ‹ฐ ์ƒ์„ฑ
    3. ์ƒ์„ฑ๋œ ๊ฐ์ฒด ๋ฐ˜ํ™˜
var foo = function () {    console.dir(this);};
// 1. ํ•จ์ˆ˜ ํ˜ธ์ถœfoo(); // window// window.foo();
// 2. ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœvar obj = {foo: foo};obj.foo(); // obj
// 3. ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ํ˜ธ์ถœvar instance = new foo(); // instance
// 4. apply/call/bind ํ˜ธ์ถœvar bar = {name: 'bar'};foo.call(bar); // barfoo.apply(bar); // barfoo.bind(bar)(); // bar

OOP#

OOP์˜ ๊ธฐ๋ณธ ์ปจ์…‰์€ ์‹ค ์„ธ๊ณ„์˜ ์ผ๋“ค์„ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ชจ๋ธ๋งํ•˜๊ณ , ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์œผ๋ฉด ์–ด๋ ค์šธ ์ผ๋“ค์„ ์‰ฝ๊ฒŒ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

์šฉ์–ด ์ •๋ฆฌ

๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ์‹ธ๋‹ค : encapsulate ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๊ธฐ ์œ„ํ•œ ์ด๋ฆ„ : namespace ์ถ”์ƒํ™” : ์ค‘์š”ํ•œ ๊ฒƒ๋“ค๋งŒ ๋ฝ‘์•„์„œ ๋‹จ์ˆœํ•œ ๋ชจ๋ธ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์ž‘์—…์ธ์Šคํ„ด์Šค : ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด ๋งŒ๋“ค์–ด์ง„ ์‹ค์ œ ๊ฐ์ฒด (ํด๋ž˜์Šค๋Š” ์ฒญ์‚ฌ์ง„, ๋„๋ฉด, ํ‹€. ์ธ์Šคํ„ด์Šค๋Š” ์ฐ์–ด์ ธ ๋‚˜์˜จ ๊ฒฐ๊ณผ๋ฌผ) ๋‹คํ˜•์„ฑ : polymorphism. ์—ฌ๋Ÿฌ ๊ฐ์ฒด ํƒ€์ž…์— ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋Š” ๋Šฅ๋ ฅ

์ƒ์„ฑ์ž์™€ ๊ฐ์ฒด ์ธ์Šคํ„ด์Šค#

  1. ์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐ์ฒด ์ƒ์„ฑ: ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ๊ตณ์ด ๋นˆ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ ๋‚ด์šฉ์„ ์ฑ„์›Œ์„œ ๋ฆฌํ„ดํ•ด์•ผ ํ•˜๋Š”๊ฐ€?
function createNewPerson(name) {    var obj = {};    obj.name = name;    obj.greeting = function () {        alert("Hi! I'm " + this.name + '.');    };    return obj;}
var salva = createNewPerson('Salva');salva.name;salva.greeting();
  1. ์ƒ์„ฑ์ž ํ•จ์ˆ˜

new ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋™์ž‘ํ•œ๋‹ค.

function Person(name) {    // this = {};  (๋นˆ ๊ฐ์ฒด๊ฐ€ ์•”์‹œ์ ์œผ๋กœ ๋งŒ๋“ค์–ด์ง)
    // ์ƒˆ๋กœ์šด ํ”„๋กœํผํ‹ฐ๋ฅผ this์— ์ถ”๊ฐ€ํ•จ    this.name = name;    this.greeting = function () {        alert(`Hi! I'm ${this.name}.`);    };
    // return this;  (this๊ฐ€ ์•”์‹œ์ ์œผ๋กœ ๋ฐ˜ํ™˜๋จ)}
var salva = new Person('Salva');salva.name;salva.greeting();

๊ฐ๊ฐ์€ ์„œ๋กœ ๋‹ค๋ฅธ namespace์— ์ €์žฅ๋˜์–ด์žˆ๋‹ค. ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์†Œ๋“œ๋“ค์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด, person1 ๋˜๋Š” person2๋กœ๋ถ€ํ„ฐ ํ˜ธ์ถœํ•˜์—ฌ์•ผ ํ•œ๋‹ค. ๋‘ ๊ฐ์ฒด์˜ ๊ธฐ๋Šฅ์€ ๋”ฐ๋กœ ํŒจํ‚ค์ง•๋˜์–ด์„œ๋กœ ์ถฉ๋Œํ•˜์ง€ ์•Š์„ ๊ฒƒ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‘ Person ๊ฐ์ฒด๋Š” ๊ฐ๊ฐ ๊ณ ์œ ์˜ name ํ”„๋กœํผํ‹ฐ์™€ greeting() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ๋‘˜์ด ์ƒ์„ฑ๋  ๋•Œ ๋ถ€์—ฌ๋ฐ›์€ ์ž์‹ ์˜ name ๊ฐ’์„์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฒƒ์— ์ฃผ๋ชฉํ•˜์ž. ์ด๊ฒƒ์ด this๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋งค์šฐ ์ค‘์š”ํ•œ ์ด์œ  ์ค‘ ํ•˜๋‚˜์ด๋‹ค. ๊ฐ์ฒด๋“ค์€ ๋‹ค๋ฅธ ๊ฐ’์ด ์•„๋‹ˆ๋ผ, ๊ทธ๋“ค์ด ๊ฐ€์ง„ ๊ณ ์œ ์˜ ๊ฐ’์„ ์‚ฌ์šฉํ•œ๋‹ค.

๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด๊ณผ ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋ฐฉ์‹์˜ ์ฐจ์ด#

// ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด ๋ฐฉ์‹var foo = {    name: 'foo',    gender: 'male',};
console.dir(foo);
// ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋ฐฉ์‹function Person(name, gender) {    this.name = name;    this.gender = gender;}
var me = new Person('Lee', 'male');console.dir(me);
var you = new Person('Kim', 'female');console.dir(you);

๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด ๋ฐฉ์‹๊ณผ ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋ฐฉ์‹์˜ ์ฐจ์ด๋Š” ํ”„๋กœํ† ํƒ€์ž… ๊ฐ์ฒด([[Prototype]])์—์žˆ๋‹ค.

๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด ๋ฐฉ์‹์˜ ๊ฒฝ์šฐ, ์ƒ์„ฑ๋œ ๊ฐ์ฒด์˜ ํ”„๋กœํ† ํƒ€์ž… ๊ฐ์ฒด๋Š” Object.prototype์ด๋‹ค.

์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋ฐฉ์‹์˜ ๊ฒฝ์šฐ, ์ƒ์„ฑ๋œ ๊ฐ์ฒด์˜ ํ”„๋กœํ† ํƒ€์ž… ๊ฐ์ฒด๋Š” Person.prototype์ด๋‹ค

์ฐธ๊ณ ์ž๋ฃŒ#

JavaScript ๊ฐ์ฒด ์†Œ๊ฐœ ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฉ์‹์— ์˜ํ•ด ๊ฒฐ์ •๋˜๋Š” this JavaScript: What is the meaning of this?