Different syntax for throw

2

I have a question that has come up to me using the keyword throw of JavaScript.

According to the documentation the syntax is:

  

throw expression;

Now, I've seen several examples using throw as if it were a call to a function, that is, the expression in parentheses:

throw(new Error('Esto es un error'))

Moreover, if we pass more than one argument, it will take the last argument:

throw(1, 2, 3, new Error('Esto es un error'), 'El último')

This behavior seems to be undocumented, the question would be, why does not some syntax error jump?

If the syntax is correct, what is the point of being able to pass as many arguments to it?

If I'm totally wrong, what am I missing here?

    
asked by Jose Hermosilla Rodrigo 02.01.2017 в 17:05
source

1 answer

3

Throw is not a function, it is a sentence therefore when you do this

throw(new Error('Esto es un error'))

you are executing the throw statement that is accompanied by an expression and not a list of parameters therefore the parentheses of the first level are unnecessary and do not indicate the execution of a function but rather here they serve as an operator of grouping , therefore you can omit them.

throw new Error('Esto es un error')

do not get confused with the syntax of a function, that to execute it you must use the parentheses and send a list of parameters

function hola(nombre) {
  console.log('Hola, ' + nombre);
}

hola('Pepe'); // Esto ejecuta la función
hola 'Pepe'; // Esto es un error de sintaxis

The parameters you send to the function are expressions

hola(('Pepe')); // Esto es igual a hola('Pepe');

here the first level of parentheses executes the function [hello], the second level of parentheses serve as a grouping operator that resolves to the same value 'Pepe'.

The functions in javascript can receive N amount of parameters, if when executing you send more parameters of the necessary ones then the function will take the first ones until it coincides with the number of parameters that it receives and discards the remainder.

hola('Pepe', 'Lucho', 28); // la función [hola] solo toma 'Pepe' e ignora el resto

but this is different from a list of expressions separated by commas such as

1, 'Hola', hola(('Pepe')), 'Lucho'; // Esto son 4 expresiones separadas por comas
function hola(nombre){console.log(nombre)}

The comma can be used to separate expressions that you write in a single line, therefore it is equivalent to this.

1; 'Hola'; hola(('Pepe')); 'Lucho';

The throw clause can receive an Error object or not, you can send any value to throw as strings, numbers or other objects.

throw new Error('Error 404 not found');
throw 'Error 404 not found';
throw 404;

So in your example this line

throw(1, 2, 3, new Error('Esto es un error'), 'El último')

is equivalent to

throw 1, 2, 3, new Error('Esto es un error'), 'El último'

As it is an expression and not a list of parameters, this expression is resolved to the last value, in your case it is the string 'The last one'.

throw (1), (2), (3), (new Error('Esto es un error')), ('El último') // Uncaught El último
throw ((1), (2), (3), (new Error('Esto es un error')), ('El último')) // Uncaught El último

and finally the semicolon delimits the sentence therefore these are not equivalent

throw 1, 2, 3, new Error('Esto es un error'), 'El último' // Uncaught El último

throw 1; 2; 3; new Error('Esto es un error'); 'El último' // Uncaught 1
    
answered by 02.01.2017 / 22:36
source