How best to implement out params in JavaScript?

后端 未结 9 1584
盖世英雄少女心
盖世英雄少女心 2020-12-15 03:36

I\'m using Javascript with jQuery. I\'d like to implement out params. In C#, it would look something like this:

/*
 * odp      the object to test
 * error            


        
9条回答
  •  一整个雨季
    2020-12-15 04:17

    The answers I have seen so far aren't implementing out parameters in JavaScript, as they are used in C# (the out keyword). They are merely a workaround that returns an object in case of an error.

    But what do you do if you really need out parameters?

    Because Javascript doesn't directly support it, you need to build something that is close to C#'s out parameters. Take a look at this approach, I am emulating C#s DateTime.TryParse function in JavaScript. The out parameter is result, and because JavaScript doesn't provide an out keyword, I am using .value inside the function to pass the value outside the function (as inspired by MDN suggestion):

    // create a function similar to C#'s DateTime.TryParse
    var DateTime = [];
    DateTime.TryParse = function(str, result) {
      result.value = new Date(str); // out value
      return (result.value != "Invalid Date");
    };
    
    // now invoke it
    var result = [];
    if (DateTime.TryParse("05.01.2018", result)) {
      alert(result.value);
    } else {
      alert("no date");
    };

    Run the snippet and you'll see it works: It parses the str parameter into a Date and returns it in the result parameter. Note that result needs to be initialized as empty array [], before you call the function. This is required because inside the function you "inject" the .value property.


    Now you can use the pattern above to write a function as the one in your question (this also shows you how to emulate the new discard parameter known as out _ in C#: In JavaScript we're passing [] as shown below):

    // create a function similar to C#'s DateTime.TryParse
    var DateTime = [];
    DateTime.TryParse = function(str, result) {
      result.value = new Date(str); // out value
      return (result.value != "Invalid Date");
    };
    
    // returns false, if odb is no date, otherwise true
    function isLegal(odp, errorObj) {
      if (DateTime.TryParse(odp, [])) { // discard result here by passing []
        // all OK: leave errorObj.value undefined and return true
        return true;
      } else {
        errorObj.value = "ODP failed test foo"; // return error
        return false;
      }
    }
    
    // now test the function
    var odp = "xxx01.12.2018xx"; // invalid date
    var errorObj = [];
    if (!isLegal(odp, errorObj)) alert(errorObj.value); else alert("OK!");

    What this example does is it uses the result parameter to pass an error message as follows:

    errorObj.value = "ODP failed test foo"; // return error

    If you run the example it will display this message in a popup dialog.

    Note: Instead of using a discard parameter as shown above, in JavaScript you could also use a check for undefined, i.e. inside the function check for

    if (result === undefined) { 
       // do the check without passing back a value, i.e. just return true or false 
    };
    

    Then it is possible to omit result as a parameter completely if not needed, so you could invoke it like

    if (DateTime.TryParse(odp)) { 
        // ... same code as in the snippet above ...
    };
    

提交回复
热议问题