call , apply , bind
두번째
==========================
let add = function(c){
console.log(this.a + this.b + c);
};
let obj = {
a:1,
b:2
};
add.call(obj , 3); // 6
함수에서 객체에 접근시키게 할수있게 call 을 사용함으로서,
값을 얻어냄
console.dir[];
let argsToArray = function(){
console.log(arguments);
};
argsToArray(1,2,3);
//
0:1
1:2
2:3
//이건 배열이 아니고 유사배열이다.
let argsToArray = function(){
console.log([].slice.call(arguments));
};
argsToArray(1,2,3);
//
0:1
1:2
2:3
이건 진짜 배열로 나온거다.
let mammal = function(){
this.legs = legs;
};
let cat = function(legs, isDomesticated){
mammal.call(this.legs);
this. isDomesticated = isDomesticated;
};
let lion = new cat (4 , false);
console.log(lion);
//
cat {legs : 4 , isDomesticated : false}
isDomesticated : false
legs : 4
let numArray = [1,2,3];
console.log(Math.min(1,2,3)); // 1
console.log(Math.min.apply(null , numArray)); // 1
let button = function(content){
this.content = content;
};
button.prototype.click = function(){
console.log(${this.content} clicked);
};
let newButton = new button('add');
let boundButton = newButton.click.bind(newButton);
boundButton();
let myObj = {
asyncGet(cb){
cb();
},
parse(){
console.log('parse called');
},
render(){
this.asyncGet(function(){
this.parse();
}.bind(this));
}
};
myObj.render();
// parse called