2014-03-25 7 views
3

Lo-Dash에서 thisArg를 어떻게 사용합니까? 체인과 결합 할 수 있습니까? 나는 그들의 문서에 하나를 찾을 수 없습니다로 Lo-Dash에서 thisArg를 사용하는 방법?

_.map(collection, [callback=identity], [thisArg]) 

Creates an array of values by running each element in the collection through the callback. The callback is bound to thisArg and invoked with three arguments; (value, index|key, collection). 

If a property name is provided for callback the created "_.pluck" style callback will return the property value of the given element. 

If an object is provided for callback the created "_.where" style callback will return true for elements that have the properties of the given object, else false. 

Aliases 

_.collect 

Arguments 

collection (Array|Object|string): The collection to iterate over. 
[callback=identity] (Function|Object|string): The function called per iteration. If a property name or object is provided it will be used to create a ".pluck" or ".where" style callback, respectively. 
[thisArg] (*): The this binding of callback. 
Returns 

(Array): Returns a new array of the results of each callback execution. 

은 평가 될 것입니다 : 이것은 그들의 문서에서 thisArg를 사용하는 예입니다.

답변

3

인수 thisArg은 콜백 컨텍스트로도 사용되는 호출자 컨텍스트 this의 포인터입니다. 편의를 위해 제공됩니다.

결과는 동일 this의 별칭 변수를 만들어 대신 사용하는 것입니다 :

_.map(collection, callback.bind(this)); 

다른 방법을 예 : 기본 Function.bind() 방법을 사용하는 경우 등.

+0

변수 'this'바인딩을 사용하려면이 인수를 사용할 수 있습니까? 예 : 'O = {foo : function() {return this.bar; }, bar : 42}; A = [O, O]; _.map (A, O.foo, _.identity)'? (마지막 표현식이 예상대로 작동하지 않습니다.) –

+0

github에서 직접 문제를 열었습니다 : https://github.com/lodash/lodash/issues/803 –