TypeScript、JavaScript如何访问lambda变量

TypeScript, JavaScript how to access lambda variable

本文关键字:访问 lambda 变量 JavaScript 何访问 TypeScript      更新时间:2023-09-26
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);
})();

我怎么能访问变量x在getPropertyName函数??

以代码回答的形式详细说明Jeremy Starcher所说的话…根据需要更改:

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