簡體   English   中英

function執行后的方括號

[英]Square Brackets after function execution

聲明 function 時的目的/它是如何工作的,例如:

myObj.request({myParam: null })[something](from, (error, res, body){
   //code
})

這段代碼的行為是什么? 我不明白[something]和隨后的匿名 function 的含義

我不知道您發布的代碼的實現細節,所以我只能猜測:

myObj.request()將返回一個函數數組/對象。 使用[something]您將 select 其中一個函數,然后調用它。 如前所述,那只是猜測發生了什么,因為您沒有提供任何上下文。 例如:

const myObj = {
  request: () => ({ 
    a: () => console.log('i am fn a'), 
    b: () => console.log('i am fn b'), 
  })
}

myObj.request()['a']() // console.logs 'i am fn a'

注意:為簡單起見,我沒有將任何參數/回調傳遞給函數。 如果您仍有問題,請告訴我,我會更新答案

Javascript 評估方括號內的表達式,在您的情況下是something的值,並將使用該名稱執行 function。

例如在你的情況下

myObj.request({myParam: null })[something]

如果something的值是"get"為字符串。 它只會執行

myObj.request({myParam: null })["get"]

或者

myObj.request({myParam: null }).get

Its just like accessing a prperty inside an object, where myObj.request({myParam: null }) is the object and value for something is the property inside that object.

暫無
暫無

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

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