What is the safe way to define a default value for a function parameter?

Asked

Viewed 498 times

3

In PHP, I can make a function have by default a value in a parameter.

Example:

function func($a = 1, $b = 2) {
   return $a + $b;
}

What about Javascript? I can make the same statement on Firefox 39;

function b (a=1) {
   return a;
}

But the recommendation I always see is:

function b (a) {
   a = a || 1;
   return a;
}
  • Why not use the default setting of a parameter, as is done in PHP, since it is possible to do this?

  • Is there a restriction on the browser version used?

Observing: When you said what is the safe way, I refer to the security that something will work in any browser, regardless of version.

6 answers

10


I went through something similar and I was told that it was more right to use the example as in Link

<script>
  function soma(a, b){
    if(typeof b == 'undefined') {
     b = 2;
    }
    document.write(a + b);
  }
  soma(2);
</script>

Just as he quotes in the post, I was told the same thing, "if I do the same in PHP only Firefox will work".

I tested and really applied only to Firefox, so for me, this is the safest method...

  • 2

    +1 Interesting. How the genius guys will do something that only works in Firefox!?!?

  • 1

    @Wallacemaxters would also like to know rsrsrs...

6

Today (2015) this is not yet possible as in PHP. But in the future, with ES6 is!

What is possible today (2015):

Nowadays, with the Javascript version browsers use this is not possible. The solution to use is to check the value within the function, as mentioned in the question.

For example:

function b(a){
   if (typeof a == 'undefined') a = 1;
   return a;
}

Note: Firefox is already implementing some of ES6’s ideas but that’s not cross-browser compatible nowadays, hence unviable in production.

With ES6 - "Default Function Parameters"

When the new version of Javascript is implemented in browsers, as Firefox is already doing in this case, the solution will be (according to what the specifications of the Ecmascript) as it is in PHP (and that Firefox is already applying).

function b (a = 1) {
   return a;
}
console.log(b()); // dá: 1
  • It is important to use logical expressions directly in the argument, such as a = a || 1 or !a because there are automatic conversions of typing that can provoke false results. I suggest always buy with undefined, including typing, using the operator ===.

  • 1

    @Mfedatto you’re right. To save confusion to those who don’t know that I edited the answer.

5

2

It is common when we know how to program in one language and we are learning another, want to implement functionalities of what we already know, however this is not the right, we must learn the new language, and its particularities.

For example I want to use explode of PHP in the javascript, can I search explode em javascript, but my result will be .split.

We must take into consideration only the aspects of language we are dealing with.

Making an analogy, we can put as follows :
Both PHP and Javascript have versions, however PHP is your boss if you decide to work with 5.4 in your server, this will be 5.4 for all your clients. Already javascript would be an "uncontrolled" version, in which it defines the client choosing which browser he wants to use.

So if you want to build a stable code for everyone, you will have to implement so that you have all the available versions.

1

I’m not sure if the statement that works on Firefox 39 works on all browsers. But the second implementation should work smoothly. Since if we call the function without parameters "b()" the variable 'a' will not have value or is 'Undefined'. This condition "a || 1 " checks if 'a' has an associated value if it does not become the default value "1". If the variable already has an associated value before that, it will only keep its value

  • The assignment a = a || 1; will assign 1 when calling b(0); because the direct logical expression will convert the 0 for false.

1

The default parameters of a function allow regular parameters to be initialized with initial values if Undefined or no value is passed.

Syntax

function [nome]([param1[ = valorPredefinido1 ][, ..., paramN[ = valorPredefinidoN ]]]) {
   instruções
}

With standard parameters, verification in the body of the function is no longer necessary. Now, you can simply put one as the default value for b in the function head:

function multiply(a, b = 1) {
  return a*b;
}

multiply(5); // 5

The compatibility to use this feature, would be the following:

inserir a descrição da imagem aqui

And to the mobile:

inserir a descrição da imagem aqui

source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Parametros_Predefinidos

  • this notation is defined by ES6, not even closely compatible with all browsers.

  • cool friend, the default parameters as asked, only work in firefox, as I demonstrated and verified with the image of the table above, so why the negative ? unnecessary, ignorance on the part of those who did this, read the source to get better information

Browser other questions tagged

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