2014-07-15 2 views

답변

3

차이점에 대한 이해를 돕기 위해 JSFiddle을 만들었습니다. 언 바운드 함수는 객체에 바인딩되지 않은 함수이므로 해당 함수의 this은 전역 (윈도우) 객체를 참조합니다. 함수를 객체의 메소드로 만들거나 명시 적으로 바인딩하려면 .bind() 메서드를 사용하여 함수를 바인딩 할 수 있습니다. 내 코드에서 다른 사례를 보여주었습니다.

// A function not bound to anything. "this" is the Window (root) object 
var unboundFn = function() { 
    alert('unboundFn: ' + this); 
}; 
unboundFn(); 


// A function that is part of an object. "this" is the object 
var partOfObj = function() { 
    alert('partOfObj: ' + this.someProp); 
}; 
var theObj = { 
    "someProp": 'Some property', 
    "theFn": partOfObj 
}; 
theObj.theFn(); 


// A function that is bound using .bind(), "this" is the first parameter 
var boundFn = function() { 
    alert('boundFn: ' + this.someProp); 
}; 
var boundFn = boundFn.bind(theObj); 
boundFn(); 
+0

고맙습니다. –

+1

문제는 없습니다. 나는 누군가가 적어도 당신에게보기 흉하지 않은 대답을하려고 노력해야한다고 생각했다. – neelsg

관련 문제