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

201106

ยท ์•ฝ 4๋ถ„

์ผ๋ฐ˜ ํ•จ์ˆ˜ vs ํ™”์‚ดํ‘œ ํ•จ์ˆ˜#

const shape = {    radius: 10,    diameter() {        return this.radius * 2;    },    arrowDiameter: () => {        return this.radius * 2;    },};
console.log(shape.diameter());// 20console.log(shape.arrowDiameter());// NaN
const diameter = shape.diameter;const arrowDiameter = shape.arrowDiameter;
console.log(diameter());// NaNconsole.log(arrowDiameter());// NaN
function Shape() {    this.radius = 10;    this.diameter = function () {        return this.radius * 2;    };    this.arrowDiameter = () => {        return this.radius * 2;    };}
const shape = new Shape();
console.log(shape.diameter());// 20console.log(shape.arrowDiameter());// 20
const diameter = shape.diameter;const arrowDiameter = shape.arrowDiameter;
console.log(diameter());// NaNconsole.log(arrowDiameter());// 20
  • ์ผ๋ฐ˜ ํ•จ์ˆ˜๋Š” ํ˜ธ์ถœํ•  ๋•Œ ๋™์ ์œผ๋กœ this ๋ฅผ ๋ฐ”์ธ๋”ฉํ•œ๋‹ค.

  • ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ์„ ์–ธํ•  ๋•Œ ์ •์ ์œผ๋กœ this ๋ฅผ ๋ฐ”์ธ๋”ฉํ•œ๋‹ค. (์–ธ์ œ๋‚˜ ์ƒ์œ„ ์Šค์ฝ”ํ”„์˜ this ๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.)

  • ์ผ๋ฐ˜ ํ•จ์ˆ˜๋Š” prototype ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์ง„๋‹ค. โ‡’ constructor ํ•จ์ˆ˜๋กœ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค.

  • ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” prototype ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์ง€ ์•Š๋‹ค. โ‡’ constructor ํ•จ์ˆ˜๋กœ์‚ฌ์šฉ๋  ์ˆ˜ ์—†๋‹ค.


์ƒ์„ฑ์ž ํ•จ์ˆ˜(constructor)์™€ ํ”„๋กœํ† ํƒ€์ž…(prototype)#

function Person(name, age) {    this.name = name;    this.age = age;}
// Person.prototype = { constructor: function Person(...) { ... } }// Person.prototype.constructor = function Person(...) { ... }
const foo = new Person('foo', 30);// foo: Person { name: 'foo', age: 30 }// foo.__proto__: { constructor: function Person(...) { ... } }// foo.__proto__.constructor = function Person(...) { ... }

2020-11-06-201106-image-0


2020-11-06-201106-image-1

โ˜ __proto__ ๋Š” ECMAScript์˜ ์ŠคํŽ™์ด ์•„๋‹Œ ์ผ๋ถ€ ๋ชจ๋˜ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ๊ตฌํ˜„ํ•œ ๊ฒƒ์œผ๋กœ, [[Prototype]] ๋ฅผ ์ ‘๊ทผํ•˜๋Š” ์†์„ฑ์ด๋‹ค.

function Person(name) {    this.name = name;    this.getName = function () {        return this.name;    };}
console.log(Person.prototype); // { constructor: function Person(name) { ... } }
function Child(name, age) {    Person.call(this, name);    this.age = age;    this.getAge = function () {        return this.age;    };}
console.log(Child.prototype); // { constructor: function Child(name, age) { ... } }
Child.prototype = Object.create(Person.prototype);
console.log(Object.create(Person.prototype)); // Person {}console.log(Child.prototype); // Person {}console.log(Child.prototype.constructor); // function Person(name) { ... }
Child.prototype.constructor = Child;
console.log(Child.prototype);// Person { constructor: function Child(name, age) { ... } }
var child = new Child('foo', 20);
console.log(child.getName()); // 'foo'console.log(child.getAge()); // 20

ํด๋ž˜์Šค ๋ฌธ๋ฒ•์€ ๋‹จ์ง€ "๊ตฌ๋ฌธ์  ์„คํƒ•" ์ธ๊ฐ€?#

class User {    constructor(name) {        this.name = name;    }
    sayHi() {        alert(this.name);    }}
let user = new User('John');user.sayHi();
function User(name) {    this.name = name;}
User.prototype.sayHi = function () {    alert(this.name);};
let user = new User('John');user.sayHi();

ES6์˜ ํด๋ž˜์Šค ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ์ผ์น˜ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๊ธฐ์กด function + prototype ๋ฌธ๋ฒ•์˜ ์กฐํ•ฉ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ผ๋ถ€ ์‚ฌ๋žŒ๋“ค์€ ๋‹จ์ง€ ํด๋ž˜์Šค ๋ฌธ๋ฒ•์„ "๊ตฌ๋ฌธ์  ์„คํƒ•" (syntax sugar)๋ผ๊ณ  ๋งํ•œ๋‹ค.

ํ•˜์ง€๋งŒ ๋ช‡ ๊ฐ€์ง€ ์ค‘์š”ํ•œ ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค.

  1. class ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•ด์„œ ๋งŒ๋“  ํ•จ์ˆ˜์—” ํŠน์ˆ˜ ๋‚ด๋ถ€ ํ”„๋กœํผํ‹ฐ์ธ [[FunctionKind]]: "classConstructor" ๊ฐ€ ์ด๋ฆ„ํ‘œ์ฒ˜๋Ÿผ ๋ถ™์–ด์„œ, ์ด ํด๋ž˜์Šค ์ƒ์„ฑ์ž๋ฅผ new ์™€ ํ•จ๊ป˜ ํ˜ธ์ถœํ•˜์ง€ ์•Š์œผ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

  2. ํด๋ž˜์Šค์˜ ๋ฉ”์†Œ๋“œ๋Š” non-enumerable ๋กœ, for ... in ๊ตฌ๋ฌธ ๋“ฑ์˜ ๊ฐ์ฒด ์ˆœํšŒ์—์„œ๋ฉ”์†Œ๋“œ๋Š” ์ˆœํšŒํ•˜์ง€ ์•Š๋Š”๋‹ค. (enumerable ํ”Œ๋ž˜๊ทธ๊ฐ€ false ์ด๋‹ค.)

  3. ํด๋ž˜์Šค๋Š” ํ•ญ์ƒ use strict ๋ชจ๋“œ์ด๋‹ค.

  4. ์ด์™ธ์—๋„ getter, setter, ๋ฏน์Šค์ธ ๋“ฑ์˜ ๊ธฐ๋Šฅ์ด ์žˆ๋‹ค.

ํด๋ž˜์Šค์™€ ํ•จ์ˆ˜์—์„œ์˜ new ์—ฐ์‚ฐ์ž ๋™์ž‘#

2020-11-06-201106-image-2