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.
@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 [...]– Guilherme Nascimento
[...] 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 :)
– Guilherme Nascimento
I now understand the criterion. Mt good.
– Sam