2016-06-28 3 views
1
interface users { 
    s:number; 
    b:string; 
} 

function getPropertyName(propertyFunction: (a:users) => any) { 
    console.log(propertyFunction.toString()); // ii => ii.s > x && ii.s < 3 
    // how to read x variable here ? 
} 

(function() { 
    var x = 4; 
    getPropertyName(ii => ii.s > x && ii.s < 3); 
})(); 

어떻게 getPropertyName 함수에서 변수 x에 액세스 할 수 있습니까 ??TypeScript, JavaScript 람다 변수에 액세스하는 방법

+0

그렇게 할 수있는 방법은 없습니다. 왜 그걸하고 싶니? – Thomas

+0

당신은 같은 범위에'x'와'getPropertyName'을 가져야합니다 .. 다른 범위, 접근 할 방법이 없습니다. 그것은 getPropertyName을 수정하거나 추가 인자를 취할 수 있습니다. –

+1

나는 ORM이라고 쓰고 싶다 :) 그리고 그것은 어디에서 함수 인수가 좋을까 ... – Pasibrzuchon

답변

1

제레미 Starcher 코드 응답의 형태로 말씀에 자세히 설명하기 위해 .. 여러분의 필요에 변경 :

function test(func: (x:any, ii:any)=> any){ 
    //do extra stuff here 
} 

var x, ii; 
test(function (x, ii) { 
    //do stuff here 
}); 
관련 문제