What is the safe way to set a default value for a function parameter? [duplicate]

3

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

Example:

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

What about JavaScript? Can I make the same statement in Firefox 39 ;

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

But the recommendation I always see is:

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

  • Is there any restriction on the browser version that is used?

Note : When you said which is the safe way , I mean the security that something will work in any browser, regardless of the version.     

asked by anonymous 02.10.2015 / 14:02

6 answers

10

I went through something similar and they told me that it was safer 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>

As he quotes in the post, they told me the same thing, "if you do the same in PHP only Firefox will work."

I tested it and it really only applied in Firefox, so for me, that's the safest method ...

    
02.10.2015 / 14:10
6

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

What is possible today (2015):

Nowadays, with the version of JavaScript that browsers use this is not possible. The solution to use is to check the value inside the function, as you 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 this is not cross-browser compatible nowadays, hence not 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 the specifications of ECMAscript ) as it is in PHP (and that Firefox is already applying).

function b (a = 1) {
   return a;
}
console.log(b()); // dá: 1
    
02.10.2015 / 16:22
5

The default value was recently introduced and few browsers support it .

The ideal is to do within the function. You should check if the value is undefined and set there a value for the parameter:

if (typeof(a) === 'undefined') a = 10;
if (typeof(b) === 'undefined') b = 20;

You have a more complete answer about this pattern .

    
02.10.2015 / 14:14
2

It is common when we know how to program in a particular language and we are learning another, want to implement functionalities that we already know, but this is not right, we must learn the new language and its peculiarities.

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

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

By analogy, we can look like this:
Both PHP and Javascript have versions, however PHP is who you are so if you decide to work with 5.4 on your server, this will be 5.4 for all your clients. Javascript would be an "uncontrolled version", in which the one who defines is the client choosing which browser he wants to use.

So if you want to build stable code for everyone, you'll have to implement it to support all available versions.

    
02.10.2015 / 14:34
1

I'm not sure if the statement that works in Firefox 39 works in all browsers. However the 2nd implementation should work without problems. Since if we call the function without parameters "b ()" the variable 'a' will not have value or be 'undefined'. This condition "a || 1" checks whether '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 just keep its value

    
02.10.2015 / 14:12
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 default parameters, checking in the function body is no longer required. Now, you can simply put one as the default value for b in the head function:

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

multiply(5); // 5

Compatibility for this feature would be as follows:

Andforthemobile:

source: link

    
02.10.2015 / 14:20