λͺ¨λ μλ°μ€ν¬λ¦½νΈ Deep Dive λμμ 18μ₯μ μ 리νμμ΅λλ€.
μΌκΈ κ°μ²΄
1. 무λͺ μ 리ν°λ΄λ‘ μμ±ν μ μλ€. μ¦, λ°νμμ μμ± κ°λ₯νλ€.
2. λ³μλ μλ£κ΅¬μ‘°μ μ μ₯ν μ μλ€.
3. ν¨μμ λ§€κ°λ³μμ μ λ¬ν μ μλ€.
4. ν¨μμ λ°νκ°μΌλ‘ μ¬μ©ν μ μλ€.
ν¨μλ μμ μΌκΈ κ°μ²΄μ 쑰건μ λͺ¨λ λ§μ‘±νλ€. λ°λΌμ ν¨μλ μΌκΈ κ°μ²΄μ΄λ€. μ΄κ²μ ν¨μλ₯Ό κ°μ²΄μ λμΌνκ² μ¬μ©ν μ μλ€λ μλ―Έμ΄λ€.
μΌλ° κ°μ²΄μ κ°μ΄ ν¨μμ λ§€κ°λ³μμ μ λ¬ν μ μμΌλ©°, ν¨μμ λ°νκ°μΌλ‘ μ¬μ©ν μλ μλ€λ κ²μ΄λ€. μ΄λ ν¨μν νλ‘κ·Έλλ°μ κ°λ₯μΌ νλ μλ°μ€ν¬λ¦½νΈμ μ₯μ μ€ νλμ΄λ€.
function square(number){
return number * number;
}
console.log(Object.getOwnPropertyDescriptors(square)); //{length :~~, name: ~~, arguments: ~~, caller:~~, prototype:~~}
//__proto__λ square ν¨μμ νλ‘νΌν°κ° μλλ€.
console.log(Object.getOwnPropertyDescriptor(square, '__proto__')); //undefined
// __proto__λ Object.property κ°μ²΄μ μ κ·Όμ νλ‘νΌν°λ€.
cosole.log(Object.getOwnPropertyDesciptor(Object.prototype, '__proto__'));
arguments, caller, length, name, prototype νλ‘νΌν°λ λͺ¨λ ν¨μ κ°μ²΄μ νλ‘νΌν°μ΄λ€. __proto__
λ μ κ·Όμ νλ‘νΌν°μ΄λ©°, ν¨μ κ°μ²΄ κ³ μ μ νλ‘νΌν°κ° μλλΌ Object.prototype κ°μ²΄μ νλ‘νΌν°λ₯Ό μμλ°μ κ²μ μ μ μλ€. Object.prototype κ°μ²΄μ νλ‘νΌν°λ λͺ¨λ κ°μ²΄κ° μ¬μ©λ°μ μ¬μ©ν μ μλ€.
arguments κ°μ²΄λ ν¨μ νΈμΆ μ μ λ¬λ μΈμλ€μ μ 보λ₯Ό λ΄κ³ μλ μν κ°λ₯ν μ μ¬ λ°°μ΄ κ°μ²΄μ΄λ©°, ν¨μ λ΄λΆμμ μ§μ λ³μμ²λΌ μ¬μ©λλ€. μ¦, ν¨μ μΈλΆμμλ μ°Έμ‘°ν μ μλ€.
μλ°μ€ν¬λ¦½νΈλ ν¨μμ λ§€κ°λ³μμ μΈμμ κ°μκ° μΌμΉνλμ§ νμΈνμ§ μλλ€. λ°λΌμ ν¨μ νΈμΆ μ λ§€κ°λ³μ κ°μλ§νΌ μΈμλ₯Ό μ λ¬νμ§ μμλ μλ¬κ° λ°μνμ§ μλλ€.
ν¨μκ° νΈμΆλλ©΄ ν¨μ λͺΈμ²΄ λ΄μμ μ묡μ μ€λ₯΄ λ§€κ°λ³μκ° μ μΈλκ³ undefined
λ‘ μ΄κΈ°νλ μ΄ν μΈμκ° ν λΉλλ€.
// μμ 18-04
function multiply(x, y){
console.log(arguments);
return x + y;
}
console.log(multiply()); // NaN
console.log(multiply(1)); // NaN
console.log(multiply(1, 2)); // 2
console.log(multiply(1, 2, 3)); // 2
μλ μ¬μ§μ 보면λͺ¨λ μΈμλ μ묡μ μΌλ‘ arguments κ°μ²΄μ νλ‘νΌν°λ‘ 보κ΄λλ κ²μ λ³Ό μ μλ€. arguments κ°μ²΄λ μΈμλ₯Ό νλ‘νΌν° κ°μΌλ‘ μμ νλ©° νλ‘νΌν° ν€λ μΈμμ μμλ₯Ό λνλΈλ€.
arguments κ°μ²΄λ λ§€κ°λ³μ κ°μλ₯Ό νμ ν μ μλ κ°λ³ μΈμ ν¨μλ₯Ό ꡬνν λ μ μ©νλ€.
// μμ 18-06
function sum(){
let res = 0;
for(let i = 0; i < arguments.length; i++){
res += arguments[i];
}
return res;
}
console.log(sum()); // 0
console.log(sum(1, 2)); //3
μ μ¬ λ°°μ΄ κ°μ²΄λ?
length νλ‘νΌν°λ₯Ό κ°μ§ κ°μ²΄λ‘ forλ¬ΈμΌλ‘ μνν μ μλ κ°μ²΄μ΄λ€.
arguments κ°μ²΄λ μ μ¬ λ°°μ΄ κ°μ²΄ μ΄λ©΄μ μ΄ν°λ¬λΈμ΄λ€. νμ§λ§ λ°°μ΄μ΄ μλλ―λ‘ λ°°μ΄ λ©μλλ₯Ό μ¬μ©ν κ²½μ° μλ¬κ° λ°μνλ€. Function.prototype.call
κ³Ό Function.prototype.apply
λ₯Ό μ¬μ©ν΄ κ°μ νΈμΆν΄μΌ νλ λ²κ±°λ‘μμ΄ μλ€. μ΄ λ²κ±°λ‘μμ ν΄κ²°νκΈ° μν΄ ES6μμλ Rest νλΌλ―Έν°λ₯Ό λμ
νλ€.
// μμ 18-08
function sum(...args){
return args.reduce((pre, cur) => pre + cur, 0);
}
console.log(sum(1, 2, 3)); // 6
caller νλ‘νΌν°λ λΉνμ€ νλ‘νΌν°μ΄κ³ μμΌλ‘λ νμ€νλ μμ λ μλ νλ‘νΌν°μ΄λ―λ‘ μ°Έκ³ λ‘ μμλμ. ν¨μ κ°μ²΄μ caller νλ‘νΌν°λ ν¨μ μμ μ νΈμΆν ν¨μλ₯Ό κ°λ¦¬ν¨λ€.
// μμ 18-09
function foo(func){
return func();
}
function bar(){
return 'caller : ' + bar.caller;
}
// λΈλΌμ°μ μμ μ€νν κ²°κ³Ό
console.log(foo(bar)); // caller : function foo(func) {...}
console.log(bar()); //caller : null
μ κ²°κ³Όλ λΈλΌμ°μ μμ μ€νν κ²°κ³Όμ΄κ³ node.js νκ²½μμ μ€ννλ©΄ λ€λ₯Έ κ²°κ³Όκ° λμ¨λ€.
ν¨μ κ°μ²΄μ length νλ‘νΌν°λ ν¨μλ₯Ό μ μν λ μ μΈν λ§€κ°λ³μμ κ°μλ₯Ό κ°λ¦¬ν¨λ€.
//μμ 18-10
function foo(){}
console.log(foo.length); // 0
function bar(x){
return x;
}
console.log(bar.length); // 1
function baz(x, y){
return x * y;
}
console.log(baz.length); // 2
arguments κ°μ²΄μ length νλ‘νΌν°μ ν¨μ κ°μ²΄μ length νλ‘νΌν° κ°μ λ€λ₯Ό μ μμΌλ―λ‘ μ£Όμν΄μΌνλ€. ν¨μ κ°μ²΄μ length νλ‘νΌν°λ λ§€κ°λ³μμ κ°μλ₯Ό κ°λ¦¬ν¨λ€.
ν¨μ κ°μ²΄μ name νλ‘νΌν°λ ν¨μ μ΄λ¦μ λνλΈλ€. name νλ‘νΌν°λ ES6μμ μ μ νμ€μ΄ λμλ€. ES5μμλ λΉ λ¬Έμμ΄μ κ°μΌλ‘ κ°μ§λ§ ES6μμλ ν¨μ κ°μ²΄λ₯Ό κ°λ¦¬ν€λ μλ³μλ₯Ό κ°μΌλ‘ κ°λλ€.
// μμ 18-11
var nameFunc = function foo(){};
console.log(namedFunc.name); // foo
var anonymousFunc = function(){};
console.log(anonymousFunc.name); // anonymousFunc
function var(){}
console.log(bar.name); // bar
λͺ¨λ κ°μ²΄λ [[Prototype]]μ΄λΌλ λ΄λΆ μ¬λ‘―μ κ°λλ€. __proto__
νλ‘νΌν°λ [[Prototype]] λ΄λΆ μ¬λ‘―μ΄ κ°λ¦¬ν€λ νλ‘ν νμ
κ°μ²΄μ μ κ·ΌνκΈ° μν΄ μ¬μ©νλ μ κ·Όμ νλ‘νΌν°μ΄λ€. λ΄λΆ μ¬λ‘―μλ μ§μ μ κ·Όν μ μκ³ κ°μ μ μΈ μ κ·Ό λ°©λ²μ μ 곡νλ κ²½μ°μ ννμ¬ μ κ·Όν μ μλ€.
// μμ 18-12
const obj = { a: 1 };
// κ°μ²΄ 리ν°λ΄ λ°©μμΌλ‘ μμ±ν κ°μ²΄μ νλ‘ν°νμ
κ°μ²΄λ Object.prototypeμ΄λ€.
console.log(obj.__proto__ === Object.prototype); // true
// κ°μ²΄ 리ν°λ΄ λ°©μμΌλ‘ μμ±ν κ°μ²΄λ νλ‘ν νμ
κ°μ²΄μΈ Object.prototypeμ νλ‘νΌν°λ₯Ό μμλ°λλ€.
// hasOwnProperty λ©μλλ Object.prototypeμ λ©μλμ΄λ€.
console.log(obj.hasOwnProperty('a')); // true
console.log(obj.hasOwnProperty('__proto__'));
μΈμλ‘ μ λ¬λ°μ νλ‘νΌν° ν€κ° κ°μ²΄ κ³ μ μ νλ‘νΌν° ν€μΈ κ²½μ°μλ§ trueλ₯Ό λ°ννκ³ μμλ°μ νλ‘ν νμ νλ‘νΌν° ν€μΈ κ²½μ° falseλ₯Ό λ°ννλ€.
μμ±μ ν¨μλ‘ νΈμΆν μ μλ ν¨μ κ°μ²΄, μ¦ constructorλ§μ΄ μμ νλ νλ‘νΌν°μ΄λ€. μΌλ° κ°μ²΄μ μμ±μ ν¨μλ‘ νΈμΆν μ μλ non-constructorμλ prototype νλ‘νΌν°κ° μλ€.
// μμ 18-13
(function () {}).hasOwnProperty('prototype'); // true
({}).hasOwnProperty('prototype'); // false
prototype νλ‘νΌν°λ ν¨μκ° κ°μ²΄λ₯Ό μμ±νλ μμ±μ ν¨μλ‘ νΈμΆλ λ μμ±μ ν¨μκ° μμ±ν μΈμ€ν΄μ€μ νλ‘ν νμ κ°μ²΄λ₯Ό κ°λ¦¬ν¨λ€.