standard argument in javascript function

Asked

Viewed 69 times

2

Which of these, or other, is the best way to set default value for a javascript function?

function func(arg){
   //Esse:
   arg = arg || "valor padrão";
   //Ou esse:
   if(arg === void 0 ) { arg = "valor padrão"}
   //Ou esse:
   arg = arguments.length > 0 ? arguments[0] : "valor padrão";
}

3 answers

3


The aforementioned Default parameter (default parameter/Default parameter) is a good way out:

function foo(bar = 1) {
     console.log(bar);
}

foo();
foo('oi');

Yet what no one has yet quoted is that this will not work in older browsers, as:

  • Internet Explorer (same at 11)
  • Safari 9 (or older)

And it probably won’t work on some native Android browsers, so if the intention is to support without wanting to take risks use the typeof even, or you can use IIEF (Immediately Invoked Function Expression), like this:

/*
w = window
d = document
u = undefined
*/
(function (w, d, u) {
    w.minhaFuncao = function (a, b, c) {
          a = a === u ? 'valor padrão para "a"' : a;
          b = b === u ? 'valor padrão para "b"' : b;
          c = c === u ? 'valor padrão para "c"' : c;
          
          console.log('/a => ', a, '/b => ', b, '/c => ', c);
    };

    w.minhaOutraFuncao = function (foo, bar) {
          foo = foo === u ? 1 : foo;
          bar = bar === u ? 1 : bar;
          
          console.log('/foo => ', foo, '/bar => ', bar);
    };
})(window, document);

minhaFuncao();
minhaFuncao('A');
minhaFuncao('A', 'B');
minhaFuncao('A', 'B', 'C');

minhaOutraFuncao();
minhaOutraFuncao('FOO');
minhaOutraFuncao('FOO', 'BAR');

  • @sam the criterion was to try to avoid the immediacy that had already occurred with answers usa X, usa Y, without there being any analysis on when to use something, the idea here is to try to fix things within the specific problem that other answers can cause, the correct one nor was nobody to have answered, was to have voted to close, but as it is common in the community only a few take the trouble to vote, then I find myself obliged to formulate a response to point out possible problems of the proposed solutions [...]

  • [...] It is not that the answers are problematic entirely, but it remains to explain when to use, then the AP leaves using something that has not been explained the compatibility part and its scripts start to fail because by chance his clients use old Androids or IE. In other words, he will have a headache due to lack of mentioning this, which is very important ;) ps: Just for the record, this is not a "criterion", it is an ODD situation, that is, each question has to be analyzed and answers given as well :)

  • 1

    I now understand the criterion. Mt good.

1

It would actually be like this:

function func(parametro = 'valor padrão') {

}

It’s not very good to use arg or args as a parameter name also.

1

For safety, I think you better inform in the method builder.

The example arg = arg || "valor padrão"; would generate an instability if the parameter type were Boolean or a numeric, "if you reported false or 0, it would assume a default value".

You may be using typeof or else be declaring in the method constructor, here are examples below:

In the method builder (I recommend):

function teste(param = 'valor padrão') {
     console.log(param);
}

teste(); // imprime a string valor padrão no console.
teste('oi'); // imprime a string oi no console.

Using the typeof:

function teste(param) {
     if(typeof param === 'undefined') {
          param = 'valor padrão';
     }
     console.log(param);
}

teste(); // imprime a string valor padrão no console.
teste('oi'); // imprime a string oi no console.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.