사용자 도구

사이트 도구


javascript:callbackandthis

callback function에서의 this사용

var A = /** @class */ (function () {
    function A() {
        this.i = 100;
    }
    A.prototype.a = function () {
    	console.log('함수 a호출됨');
    	console.log('i:', this.i);
    	// 1. 새로운 변수에 this
    	//var that = this;
    	//testCallback('1.', function() {
		//	    	console.log('this.i:', that.i);
		//	    });
 
		// 2. bind함수이용
		//testCallback('2', function(){
		//		console.log('this.i', this.i);}.bind(this) );
 
		// 2.1 bind함수이용
		 testCallback('2.1', this.myFun.bind(this));
    };
    A.prototype.add = function(k) {
    	this.i = this.i + k;
    }
	A.prototype.myFun = function(){
		console.log('this.i:', this.i);
	}
    return A;
}());
 
function testCallback(name, test){
	console.log('testCallback', name + '번째');
	test();
}
 
var myA = new A();
myA.add(200);
myA.a();
javascript/callbackandthis.txt · 마지막으로 수정됨: 2025/04/15 10:05 저자 127.0.0.1