#
์ผ๋ฐ ํจ์ 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(...) { ... }
โ
__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)๋ผ๊ณ ๋งํ๋ค.
ํ์ง๋ง ๋ช ๊ฐ์ง ์ค์ํ ์ฐจ์ด๊ฐ ์๋ค.
class
๋ฌธ๋ฒ์ ์ฌ์ฉํด์ ๋ง๋ ํจ์์ ํน์ ๋ด๋ถ ํ๋กํผํฐ์ธ[[FunctionKind]]: "classConstructor"
๊ฐ ์ด๋ฆํ์ฒ๋ผ ๋ถ์ด์, ์ด ํด๋์ค ์์ฑ์๋ฅผnew
์ ํจ๊ป ํธ์ถํ์ง ์์ผ๋ฉด ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.ํด๋์ค์ ๋ฉ์๋๋
non-enumerable
๋ก,for ... in
๊ตฌ๋ฌธ ๋ฑ์ ๊ฐ์ฒด ์ํ์์๋ฉ์๋๋ ์ํํ์ง ์๋๋ค. (enumerable ํ๋๊ทธ๊ฐ false ์ด๋ค.)ํด๋์ค๋ ํญ์
use strict
๋ชจ๋์ด๋ค.์ด์ธ์๋ getter, setter, ๋ฏน์ค์ธ ๋ฑ์ ๊ธฐ๋ฅ์ด ์๋ค.