簡體   English   中英

如何將“this”綁定到 function 參數中

[英]How to bind `this` into an function argument

我搜索並嘗試了幾種方法,例如直接使用app.shadow.dialogue({...}.bind(this))但收到此錯誤

未捕獲的類型錯誤:{(中間值)(中間值)}.bind 不是 function

並嘗試這樣做

let dialogue = {
  "title": {
    "text": "Kembali ke tes?",
    "description": "Anda memiliki tes yang masih berlangsung"
  },
  "actions": [
    {
      "text": "Ya",
      "class": "bg-safe",
      "icon": "refresh",
      "action": "direct/tes"
    },{
      "text": "Tidak, Lihat Hasil Sebelumnya",
      "class": "bg-danger",
      "icon": "close",
      "action": function() {
        app.shadow.stop();
        this.view();
      }
    }
  ]
};

dialogue.actions[1].action.bind(this);
app.shadow.dialogue(dialogue);

但是this.view仍然沒有進入scope

未捕獲的類型錯誤:this.view 不是 function

有沒有辦法讓this語句在 object 參數中可用?

也許你可以嘗試:

`
"action":()=> {
        app.shadow.stop();
        this.view();
      }`

希望這對你有用。

暫無
暫無

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

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