簡體   English   中英

如何在ionic3中的函數內部調用函數?

[英]How to call function inside function in ionic3?

如何在ionic3的函數內部調用函數,我已經嘗試過使用此函數,但是無法正常工作,並且在控制台“ =>”中出現錯誤,應該告訴我任何人如何解決此問題?

 shareProductWithSelectedUser(event) { var self = this; var arr1 = ""; var arr2 = ""; var arr3 = ""; // Iterating loop - continue only after callback from Firerbase. var x = 0; var loopArray(arr1, arr2, arr3) { // here calling inner function for share product with three param(userId, userTypeId, callback) self.callFuncForShareProduct(arr1[x], arr2[x], arr3[x], (function) => { // any more items in array? continue loop if(x < arr1.length) { this.loopArray(arr1, arr2, arr3); }; }); // start 'loop' loopArray(self.userIdListForShareProduct, self.userTypeIdListForShareProduct, self.userObjectListForShare); } 

首先讓我們簡化您的代碼

executeFunction(event) {
  var self = this;

  var innerFunction(args) {

      self.doSomething('somevariable', (function) =>  {
        if(/* expression */) {
          this.innerFunction(args);
         };
      });

      doAnotherFunction();
    }
}

因此,您將創建一個具有內部函數的函數,並希望內部函數調用自身,並且還希望在范圍之外調用另一個函數。

現在。 問題。

  • function是保留關鍵字,不能像在lambda中那樣使用。
  • 創建一個內部函數不能那樣工作。 使用function innerFunction(args)var innerFunction = (args) => {}

您可以使用第二個要點中的兩種方法重寫代碼。 我將向您展示lambda的示例。

executeFunction(event) {
  var innerFunction = (args) => {

      // the lambda allows you to keep the original scope, so you can still use `this`
      this.doSomething('somevariable', () =>  {
        if(/* expression */) {

           // innerFunction isn't defined globally but scope is maintained so you can call it like so
          innerFunction(args);
         };
      });

      this.doAnotherFunction();
    }
}

因此,在對代碼進行了兩次重復之后,我不確定是否要創建內部函數,但是上面的代碼也應該解決您的其他問題。

暫無
暫無

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

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