์ƒํ™œ์ฝ”๋”ฉ๊ฐ์ฒด์ง€ํ–ฅ

bind() bind()๋Š” this ๊ฐ’์„ ๊ณ ์ •์‹œํ‚ค๋Š” ๋ช…๋ น! bind() ์•ˆ์˜ ์ฒซ๋ฒˆ์งธ ์ธ์ž๋Š” this ๋ฅผ ๊ณ ์ •์‹œํ‚ค๊ณ , ๋‘๋ฒˆ์งธ ์ธ์ž๋ถ€ํ„ฐ๋Š” ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋ ๋•Œ๋งˆ๋‹ค ์‚ฌ์šฉ๋œ๋‹ค. sum์ด๋ผ๋Š” ํ•จ์ˆ˜์— this๋ฅผ kim ์œผ๋กœ ๊ณ ์ •ํ•˜๋Š” ์ƒˆ๋กœ์šด ํ•จ์ˆ˜(KimSum)๊ฐ€ ๋งŒ๋“ค์–ด์ง // object ์™€ function ์‚ฌ์ด์˜ ๊ด€๊ณ„๋ฅผ ๊ทœ๋ช…ํ•ด๋ณด๊ธฐ 2 let kim = {name:'kim', first:10, second:20} let lee = {name:'lee', first:10, second:10} function sum(prefix){ return prefix +(this.first + this.second); } console.log('sum.call(kim)', sum.call(kim, '=>')); console.log('..
call()์€ ์‹คํ–‰ํ• ๋•Œ๋งˆ๋‹ค ์–ด๋–ค ๊ฐ์ฒด์˜ this ๊ฐ’์„ ๋ฐ”๊พธ๋Š” ๋ช…๋ น! call()์€ ์ฒซ๋ฒˆ์งธ ์ธ์ž๋กœ๋Š” ๊ทธ ํ•จ์ˆ˜์— ๋‚ด๋ถ€์ ์œผ๋กœ this๋ฅผ ๋ญ˜๋กœ ํ• ์ง€ ์˜ค๊ณ , ๋‘๋ฒˆ์งธ ์ธ์ž๋ถ€ํ„ฐ๋Š” ํ˜ธ์ถœํ•˜๋ ค๋Š” ํ•จ์ˆ˜์— ์ธ์ž๊ฐ’(parameter)์ด ๋“ค์–ด์˜ค๊ฒŒ ๋œ๋‹ค // object ์™€ function ์‚ฌ์ด์˜ ๊ด€๊ณ„๋ฅผ ๊ทœ๋ช…ํ•ด๋ณด๊ธฐ 1 //์„œ๋กœ ์•„๋ฌด๋Ÿฐ ๊ด€๋ จ์ด ์—†๋Š” ๋‘ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์—ˆ์–ด์š”. let kim = {name:'kim', first:10, second:20} let lee = {name:'lee', first:10, second:10} function sum(prefix){ return prefix +(this.first + this.second); // ์–ด๋–ค ๊ฐ์ฒด์—๋„ ์†ํ•ด์žˆ์ง€ ์•Š์€ ํ•จ์ˆ˜ } //sum ์ด๋ผ๋Š” ๊ฐ์ฒด๋ฅผ ์‹คํ–‰์‹œํ‚ค๊ฒ ๋‹ค! consol..
์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ต‰์žฅํžˆ ์œ ์—ฐํ•œ ํŠน์ง•! ๋งˆ์Œ๋Œ€๋กœ ๋ˆ„๊ตฐ๊ฐ€์˜ ์ž์‹์ด ๋  ์ˆ˜ ์žˆ๋‹ค~~ ๋‹จ, ์ž์‹์€ ๋ถ€๋ชจ๋ฅผ ๋ชป๋ฐ”๊พผ๋‹ค let superObj = {superVal:'super'} let subObj = {subVal:'sub'} // ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ต‰์žฅํžˆ ์œ ์—ฐํ•œ ํŠน์ง•! // ๋งˆ์Œ๋Œ€๋กœ ๋ˆ„๊ตฐ๊ฐ€์˜ ์ž์‹์ด ๋  ์ˆ˜ ์žˆ๋‹ค subObj.__proto__ = superObj; // '__proto__'๋ผ๋Š” ๋งํฌ๋ฅผ ๊ฑธ์–ด์คŒ: subObj๋Š” ์„ ์กฐ๋Š” superObj์ด๋‹ค console.log('subObj.subVal =>',subObj.subVal); console.log('subObj.superVal =>',subObj.superVal);// ๊ฐ์ฒด subObj๊ฐ€ superVal์ด๋ผ๋Š” ์†์„ฑ์„ ๊ฐ€์ง€๋Š” ์ง€ ์ฐพ์•„๋ด„. ์—†๋‹ค, ๊ทธ๋Ÿฌ๋ฉด '__pro..
Object.create() __proto__ ๋Œ€์‹ ์— 'Object.create()' ๋ฅผ ์‚ฌ์šฉํ•ด์„œ prototype link(๋ช…ํ™•ํ•œ ์ƒ์†๊ด€๊ณ„)๋ฅผ ์ง€์ •ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค. let lee = Object.create(kim); kim์„ ๋ถ€๋ชจ๋กœ ํ•˜๋Š” ์ž์‹๊ฐ์ฒด lee๋ฅผ ๋งŒ๋“ค์–ด์ค˜ let superObj = {superVal:'super'} //let subObj = {subVal:'sub'} //subObj.__proto__ = superObj; let subObj = Object.create(superObj);// superObj๋ฅผ ๋ถ€๋ชจ๋กœ ํ•˜๋Š” ์ƒˆ๋กœ์šด ๊ฐ์ฒด,๋ณด๋‹ค prototype link(๋ช…ํ™•ํ•œ ์ƒ์†๊ด€๊ณ„)๋ฅผ ํ™•๋ฆฝ! subObj.subVal = 'sub'; //์ฃผ์„์ฒ˜๋ฆฌ๋ฅผ ํ•œ ์œ„์˜ ์ฝ”๋“œ์™€ ๋˜‘๊ฐ™์ด ๋™์ž‘ console.lo..
'__proto__' lee.__proto__ = kim; lee๋Š” kim์˜ ์ž์‹์ด๋‹ค // class ๋ฌธ๋ฒ•์ด ์•„๋‹ˆ๋ผ ์•„์ฃผ ๊ด€์Šต์ (prototype)์œผ๋กœ ์ƒ์†ํ•˜๋Š” ๋ฐฉ๋ฒ• let superObj = {superVal:'super'} let subObj = {subVal:'sub'} // ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ต‰์žฅํžˆ ์œ ์—ฐํ•œ ํŠน์ง•! // ๋งˆ์Œ๋Œ€๋กœ ๋ˆ„๊ตฐ๊ฐ€์˜ ์ž์‹์ด ๋  ์ˆ˜ ์žˆ๋‹ค subObj.__proto__ = superObj; // '__proto__'๋ผ๋Š” ๋งํฌ๋ฅผ ๊ฑธ์–ด์คŒ: subObj๋Š” ์„ ์กฐ๋Š” superObj์ด๋‹ค console.log('subObj.subVal =>',subObj.subVal); console.log('subObj.superVal =>',subObj.superVal);// ๊ฐ์ฒด subObj๊ฐ€ superVa..
super ํ‚ค์›Œ๋“œ super() ํ‚ค์›Œ๋“œ๋Š” ์ƒ์œ„(๋ถ€๋ชจ)๊ฐ์ฒด์˜ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค ๋‹จ, constructor(์ƒ์„ฑ์ž) ์•ˆ์—์„œ๋Š” super ํ‚ค์›Œ๋“œ ํ•˜๋‚˜๋งŒ ์‚ฌ์šฉ๋˜๊ฑฐ๋‚˜ this ํ‚ค์›Œ๋“œ ์ „์— ๋จผ์ € ํ˜ธ์ถœ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ์˜์—ญ์„, ์ž์‹ํด๋ž˜์Šค(๋‚˜)๋„ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ! ์ค‘๋ณต์„ ์ค„์ด๋ ค๊ณ  ๊ทธ๋•Œ, ์‚ฌ์šฉํ•˜๋Š” ํ‚ค์›Œ๋“œ 'super' super ํ˜•ํƒœ 2๊ฐ€์ง€ 1.super() = ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž(constructor) 2.super. = ๋ถ€๋ชจํด๋ž˜์Šค์˜ class Person { constructor(name, first, second){ this.name =name; this.first=first; this.second=second; } sum(){ return this.first+this.second; } ..
Class inheritance ์ƒ์†์„ ์“ฐ๋Š” ์ด์œ : ์ค‘๋ณต์ฝ”๋“œ๋ฅผ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•จ. ์ฆ‰, ๋ถ€๋ชจํด๋ž˜์Šค๋ฅผ ์ผ์ผ์ด ์“ฐ์ง€์•Š๊ณ , ๋ฐ๋ ค์™€ ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์„ ์ž์‹ class์— ์ถ”๊ฐ€ํ•ด ์ฃผ๊ณ  ์‹ถ์„๋•Œ ํ˜•ํƒœ : ์ž์‹ํด๋ž˜์Šค์ธ PersonPlus ๋Š” ๋ถ€๋ชจํด๋ž˜์Šค์ธ Person์˜ ์†์„ฑ์„ ๊ฐ€์ง€๊ณ  ์™€์„œ ๊ณต์œ ํ•œ๋‹ค ๋”ฐ๋ผ์„œ, ๋ถ€๋ชจํด๋ž˜์Šค Person์˜ ์†์„ฑ์„ ์ˆ˜์ •ํ•œ๋‹ค๋ฉด, ์ž์‹ํด๋ž˜์Šค PersonPlus ์†์„ฑ๋„ ์ˆ˜์ •๋จ //๋ถ€๋ชจํด๋ž˜์Šค class Person { constructor(name, first, second){ this.name =name; this.first=first; this.second=second; } sum=()=>{ return (this.first+this.second); } } //์ž์‹ํด๋ž˜์Šค class PersonPlus exte..
becky(์ง€์€)
'์ƒํ™œ์ฝ”๋”ฉ๊ฐ์ฒด์ง€ํ–ฅ' ํƒœ๊ทธ์˜ ๊ธ€ ๋ชฉ๋ก