let superObj = {
superVal: 'super'
}
let subObj = {
subVal: 'sub'
}
subObj.__proto__ = superObj;
console.log(subObj.superVal) // 'super'
📌proto: 객체를 다른 객체의 자식 객체로 만듦
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, 100));//130
console.log(sum.call(lee, 1000));//1020
📌 fn.call()
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, 100));
console.log(sum.call(lee, 1000));
let newKim = sum.bind(kim, 3000);
console.log(newKim())
📌fn.bind()
🔎 bind vs call
let superObj = {superVal: 'super'}
let subObj = Object.create(superObj)
subObj.subVal = 'sub';
debugger;
console.log(subObj.subVal);
console.log(subObj.superVal);
subObj.superVal= 'sub';
console.log(superObj.superVal)
let kim = {
name: 'kim',
first: 10, second: 20,
sum: function(){return this.first + this.second}
}
let lee = Object.create(kim);
lee.name = 'lee';
lee.first = 10;
lee.second = 10;
lee.avg = function(){
return (this.first + this.second)/2;
}
console.log(lee.sum())
console.log(lee.avg())
📌subObj = object.create(superObj)