The 'new' keyword - Object Creation in JavaScript
youtube - new ํค์๋๋ฅผ ์ง์ ๊ตฌํํ๋ ์์์ด๋ค.
function Person(saying) {
this.saying = saying,
/* return {
dumbObject: true;
} */
}
Person.prototype.talk = function() {
cosole.log('I say:', this.saying);
};
function spawn(constructor) {
var obj = {};
Object.setPrototypeOf(obj, constructor.prototype);
var argsArray = Array.prototype.slice.apply(arguments);
return consturctor.apply(obj, argsArray.slice(1)) : obj;
}
var crockford = new Person('SEMICOLAMS!!!1one1');
crokford.talk();
key point
Object.setPrototypeOf(obj, constructor.prototype);
obj์์ ์ธ์๋ก ๋ฐ์ ๊ฐ์ฒด์ prototype์ ํ ๋น ํ์๋ค.
return consturctor.apply(obj, argsArray.slice(1)) : obj;
return
์ด ๋๊ฐ์ง์ธ ์ด์ ๋ Person์์ return ์ด ์๋ ๊ฒฝ์ฐ์ ์๋ ๊ฒฝ์ฐ ๋๋ฌธ์ด๋ค.
๋ง์ฝ return์ด ์๋๊ฒฝ์ฐ๋ฉด crokford.talk();
๋ ์คํ๋์ง ์๋๋ค. obj๋ฅผ ๋ฆฌํดํ๊ฒ ์๋๊ธฐ ๋๋ฌธ์ด๋ค. newํค์๋๋ฅผ ์ฌ์ฉํด๋ ์ค์ ๋์ผํ๋ค.
Last updated
Was this helpful?