2017-08-31 59 views
-1

我有一個孩子的功能alertMSGS所以alertMSGS功能檢查中的四個變量的值,並給出一個警告消息,他們是不相等的,所以它會返回false父功能關閉功能,是DoSave()它的一個閉包函數,從我的知識(Begginer to Javascript)。所以如果返回是錯誤的,我需要退出保存,否則需要執行保存。 !見下文 。 許多感謝提前退出時,如果條件返回false

function alertMSGS(){ 
     var currentWeekEarning = parseFloat($("#_fid_19").val()); 
     var totalYearEarning= parseFloat($("label[for=_fid_39]").next("div").text().replace('$', '').replace(',', '')); 
     var currentLoss = parseFloat($("#_fid_20").val()); 
     var totalYearLoss= parseFloat($("label[for=_fid_38]").next("div").text().replace('$', '').replace(',', '')) 
     if((currentWeekEarning != totalYearEarning)){ 
     alert("currentWeekEarning Not Equal to totalYearEarning"); 
     return false; 

     } 
     if (currentLoss != totalYearLoss) { 
     alert(' currentLoss Not Equal to totalYearLoss'); 
     return false; 
     } 
    } 


    DoSave = (function(fn){ 
     return function(){ 
     var resultofalertMSGS = alertMSGS(); 
     if (resultofalertMSGS === false) { 
     // so if the function returns false then i need to exit from the (DoSave Function) and if it satisfies then i need to perform the (DoSave Function) 
     } 
     var result=fn.apply(fn, arguments); 
     return result; 
     } 
    })(DoSave); 
+0

對不起,但是,你爲什麼要嘗試做這麼複雜的事情?你不能在'alertMSGS'函數外面寫'DoSave'函數並調用它嗎? – Fefux

+0

是啊,我同意fefux – Sindhoor

回答

0

這只是一個想法,你可以實現它。嘗試。

DoSave = (function(fn){ 

     var resultofalertMSGS = alertMSGS(); 
     if (resultofalertMSGS) { 
     (DoSave); // do here what u want to do for saving data 

     } 

     var result=fn.apply(fn, arguments); // decide whether it should come inside if or else 
     return result; 
     } 
    }) 
+0

我收到錯誤fn.apply不是函數 –

+0

實際上是在那裏在你的代碼。我完全不知道爲什麼以及如何使用該功能。 – Sindhoor

+0

但它工作正常,當我的console.log試過我得到的回報爲假。所以最後想到的只是退出DoSave功能。你可以提出任何alernative方式 –

0

稍微更改了代碼,爲我工作。任何方式謝謝

DoSave = (function(fn){ 
     return function(){ 
     var resultofalertMSGS = alertMSGS(); 
     if (resultofalertMSGS === false) { 
      return false;  
     }else{ 
     var result=fn.apply(fn, arguments); 
     return result; 
     } 
    } 
    })(DoSave);