JS_call , apply , bind _2

dev.dave·2023년 7월 29일

Javascript

목록 보기
142/167

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

profile
🔥개인 메모 / 다른블로그 자료 참조 / 다른블로그 자료 퍼옴 (출처표기) /여기저기서 공부 했던 내용 개인메모 & 참고 / 개인 기록 용도 블로그 입니다.🔥

0개의 댓글