-
Notifications
You must be signed in to change notification settings - Fork 0
inside_javascript_2
์๋ฐ์คํฌ๋ฆฝํธ์ ๋ชจ๋ ๊ฐ์ฒด๋ ์์ ์ ๋ถ๋ชจ์ธ ํ๋กํ ํ์
๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๋ ์ฐธ์กฐ ๋งํฌ ํํ์ ์จ๊ฒจ์ง ํ๋กํผํฐ๊ฐ ์๋ค.
ECMAScript์์๋ ์ด๋ฌํ ๋งํฌ๋ฅผ ์๋ฌต์ ํ๋กํ ํ์
๋งํฌ(implicit prototype link) ๋ผ๊ณ ๋ถ๋ฅด๋ฉฐ ์ด๋ฌํ ๋งํฌ๋
๋ชจ๋ ๊ฐ์ฒด์ [[Prototype]] ํ๋กํผํฐ์ ์ ์ฅ๋๋ค.
์๋ฐ์คํฌ๋ฆฝํธ์ ์์ฑ ๊ท์น์ ๋ชจ๋ ๊ฐ์ฒด๋ ์์ ์ ์์ฑํ ์์ฑ์ ํจ์์ prototype ํ๋กํผํฐ๊ฐ ๊ฐ๋ฆฌํค๋ ํ๋กํ ํ์
๊ฐ์ฒด๋ฅผ
์์ ์ ๋ถ๋ชจ ๊ฐ์ฒด๋ก ์ค์ ํ๋ [[Prototype]] ํ๋กํผํฐ๋ก ์ฐ๊ฒฐํ๋ค. ([[Prototype]] ๋งํฌ)
ํ๋กํ ํ์
์ฒด์ด๋์ด๋ ํน์ ๊ฐ์ฒด์ ํ๋กํผํฐ๋ ๋ฉ์๋์ ์ ๊ทผํ๋ ค๊ณ ํ ๋, ํด๋น ๊ฐ์ฒด์ ์ ๊ทผํ๋ ค๋ ํ๋กํผํฐ๋ ๋ฉ์๋๊ฐ ์๋ค๋ฉด
[[Prototype]] ๋งํฌ๋ฅผ ๋ฐ๋ผ ์์ ์ ๋ถ๋ชจ ์ญํ ์ ํ๋ ํ๋กํ ํ์
๊ฐ์ฒด์ ํ๋กํผํฐ๋ฅผ ์ฐจ๋ก๋๋ก ๊ฒ์ํ๋ ๊ฒ์ ๋งํ๋ค.
ํ๋กํ ํ์ ์ฒด์ด๋์ ํตํด ์์ ์ ํ๋กํผํฐ๋ฟ๋ง ์๋๋ผ, ์์ ์ ๋ถ๋ชจ์ญํ ์ ํ๋ ํ๋กํ ํ์ ๊ฐ์ฒด์ ํ๋กํผํฐ์๋ ์ ๊ทผ์ด ๊ฐ๋ฅํ๋ค.
-
๊ฐ์ฒด ๋ฆฌํฐ๋ด ๋ฐฉ์์์์ ํ๋กํ ํ์ ์ฒด์ด๋ ๊ฐ์ฒด ๋ฆฌํฐ๋ด๋ก ์์ฑํ ๊ฐ์ฒด๋ Object()๋ผ๋ ๋ด์ฅ ์์ฑ์ ํจ์๋ก ์์ฑ๋๋ค.
-
์์ฑ์ ํจ์๋ก ์์ฑ๋ ๊ฐ์ฒด์ ํ๋กํ ํ์ ์ฒด์ด๋
์์ฑ์ ํจ์๋ก ์์ฑํ ๊ฐ์ฒด๋ ์์ฑ์ ํจ์์ prototype ํ๋กํผํฐ๊ฐ ๊ฐ๋ฆฌํค๋ ๊ฐ์ฒด๋ฅผ ์์ ์ ํ๋กํ ํ์ ๊ฐ์ฒด๋ก ์ทจ๊ธํ๋ค.function Person(name, age, hobby) { this.name = name; this.age = age; this.hobby = hobby; } var foo = new Person('foo', 30, 'tennis'); // ํ๋กํ ํ์ ์ฒด์ด๋ (foo > Person.prototype > Object.prototype.hasOwnProperty) console.dir(foo.hasOwnProperty('name')); // ture console.dir(Person.prototype);
foo์ ๊ฐ์ฒด ์์ฑ์ ํจ์๋ Person์ด๋ค.
foo ๊ฐ์ฒด์ ํ๋กํ ํ์ ๊ฐ์ฒด๋ ์์ ์ ์์ฑํ Person ์์ฑ์ ํจ์ ๊ฐ์ฒด์ prototype ํ๋กํผํฐ๊ฐ ๊ฐ๋ฆฌํค๋ ๊ฐ์ฒด๊ฐ ๋๋ค.
์ฆ, foo ๊ฐ์ฒด์ ํ๋กํ ํ์ ๊ฐ์ฒด๋ Person.prototype์ด ๋๋ค.๊ฐ์ฒด ๋ฆฌํฐ๋ด ๋ฐฉ์์ด๋ ์์ฑ์ ํจ์๋ฅผ ์ด์ฉํ ๋ฐฉ์์ด๋ ๊ฒฐ๊ตญ์ Object.prototype์์ ํ๋กํ ํ์ ์ฒด์ด๋์ด ๋๋๋ค.
-
ํ๋กํ ํ์ ๋ ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์ฒด๋ค ํจ์๊ฐ ์์ฑ๋ ๋ ์์ ์ prototype ํ๋กํผํฐ์ ์ฐ๊ฒฐ๋๋ ํ๋กํ ํ์ ๊ฐ์ฒด๋ ๋ํดํธ๋ก constructor ํ๋กํผํฐ๋ง์ ๊ฐ์ง ๊ฐ์ฒด๋ค.
๋ฐ๋ผ์, ์ผ๋ฐ ๊ฐ์ฒด์ฒ๋ผ ๋์ ์ผ๋ก ํ๋กํผํฐ๋ฅผ ์ถ๊ฐ/์ญ์ ํ๋ ๊ฒ์ด ๊ฐ๋ฅํ๋ค. ๊ทธ๋ฆฌ๊ณ ๋ณ๊ฒฝ๋ ํ๋กํผํฐ๋ ์ค์๊ฐ์ผ๋ก ํ๋กํ ํ์ ์ฒด์ด๋์ ๋ฐ์๋๋ค.function Person(name) { this.name = name; } var foo = new Person('foo'); // foo.sayHello() // Error! Person.prototype.sayHello = function() { console.log('Hello'); } foo.sayHello(); // Hello
-
ํ๋กํ ํ์ ๋ฉ์๋์ this ๋ฐ์ธ๋ฉ
ํ๋กํ ํ์ ๋ฉ์๋๋ _๊ฐ์ฒด์ ๋ฉ์๋๋ฅผ ํธ์ถํ ๋ this ๋ฐ์ธ๋ฉ ๊ท์น_์ด ์ ์ฉ๋๋ค.function Person(name) { this.name = name; } // getName() ํ๋กํ ํ์ ๋ฉ์๋ ์ถ๊ฐ Person.prototype.getName = function () { return this.name; }; var foo = new Person('foo'); console.log(foo.getName()); // foo // Person.prototype ๊ฐ์ฒด์ name ํ๋กํผํฐ ๋์ ์ถ๊ฐ Person.prototype.name = 'person'; console.log(Person.prototype.getName()); // person
-
๋ํดํธ ํ๋กํ ํ์ ๋ณ๊ฒฝ
๋ํดํธ ํ๋กํ ํ์ ๊ฐ์ฒด๋ ํจ์๊ฐ ์์ฑ๋ ๋ ๊ฐ์ด ์์ฑ๋๋ฉฐ, ํจ์์ prototype ํ๋กํผํฐ์ ์ฐ๊ฒฐ๋๋ค.
์๋ฐ์คํฌ๋ฆฝํธ๋ ์ด๋ ๊ฒ ์์ฑ๋๋ ๋ํดํธ ํ๋กํ ํ์ ๊ฐ์ฒด๋ฅผ ๋ค๋ฅธ ์ผ๋ฐ ๊ฐ์ฒด๋ก ๋ณ๊ฒฝ ๊ฐ๋ฅํ๋ฉฐ, ์ด ํน์ฑ์ ์ด์ฉํด ๊ฐ์ฒด์งํฅ์ ์์์ ๊ตฌํํ๋ค.์์ฑ์ ํจ์์ ํ๋กํ ํ์ ๊ฐ์ฒด๊ฐ ๋ณ๊ฒฝ๋๋ฉด, ๋ณ๊ฒฝ๋ ์์ ์ดํ์ ์์ฑ๋ ๊ฐ์ฒด๋ค๋ง
[[Prototype]]๋งํฌ๋ฅผ ์ฐ๊ฒฐํ๋ค.function Person(name) { this.name = name; } console.log(Person.prototype.constructor); // Person(name) var foo = new Person('foo'); console.log(foo.country); // undefined // ๋ํดํธ ํ๋กํ ํ์ ๊ฐ์ฒด ๋ณ๊ฒฝ Person.prototype = { country: 'Korea' }; console.log(Person.prototype.constructor); // Object() var bar = new Person('bar'); console.log(foo.country); // undefined console.log(bar.country); // Korea console.log(foo.constructor); // Person(name) console.log(bar.constructor); // Object()
-
๊ฐ์ฒด์ ํ๋กํผํฐ์ ์ฝ๊ธฐ๋ ๋ฉ์๋๋ฅผ ์คํํ ๋๋ง ํ๋กํ ํ์ ์ฒด์ด๋ ๋์ํ๋ค.
function Person(name) { this.name = name; } Person.prototype.country = 'Korea'; var foo = new Person('foo'); var bar = new Person('bar'); console.log(foo.country); // Korea console.log(bar.country); // Korea // foo ๊ฐ์ฒด์ ์๋ก์ด ํ๋กํผํฐ๋ฅผ ๋์ ์ผ๋ก ์ถ๊ฐ foo.country = 'USA'; console.log(foo.country); // USA console.log(bar.country); // Korea