簡體   English   中英

Javascript:如何訪問存儲的數組中的當前索引值 function

[英]Javascript: How to access the current index value inside an array stored function

如果 function 存儲在數組項中,如何訪問 function 中數組的當前索引值?
例如,如何設置我的 function 以獲得與此類似的結果?

arr['hello'](); // function returns 'hello'
arr['world'](); // function returns 'world'
arr[123]() // function returns 123

除非您事先知道將使用哪些特定屬性,否則您將需要一個代理。

 const prox = new Proxy( {}, { get(_, prop) { return () => prop; } } ); console.log(prox[123]()); console.log(prox.hello()); console.log(prox['world']());

由於您有一些非數字屬性,因此不應使用數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM