I am looking at a open source javascript application, specifically an extension for firefox.
I am seeing this syntax in multiple places that I do not know what it means if anyone can shed some light on this.
such as..
return (...args)
or...
console.info(message, ...args.slice(1));
any idea what this '...' does? Is it like getting the third argument in or what? Third argument back? Its hard to try and debug this without being able to understand it.
JavaScript is the Programming Language for the Web. JavaScript can update and change both HTML and CSS. JavaScript can calculate, manipulate and validate data.
Javascript is used by programmers across the world to create dynamic and interactive web content like applications and browsers. JavaScript is so popular that it's the most used programming language in the world, used as a client-side programming language by 97.0% of all websites.
Usually when you encounter $() , that means the developer is using a javascript library, such as jQuery. The $ symbol is the namespace for those libraries. All the functions they define begin with $. , such as $. get() .
The primary use of JavaScript is to build web-based applications. Some of what JavaScript can do for the web includes: adding interactive behavior to web pages like zooming in and out or playing audio/video. creating web and mobile apps, the most popular examples consist of the likes of Netflix and Uber.
It's an Ecmascript 6 "rest" parameter. When used as a parameter or argument, it lets you receive or pass an array as individual arguments.
http://ariya.ofilabs.com/2013/03/es6-and-rest-parameter.html
It will unpack an array (args) into a formal argument list. Amongst other things this allows the members of a rest parameter to be passed as a set of formal arguments to another function.
Here's an example:
var stats = function(...numbers) {
for (var i=0, total = 0, len=numbers.length; i<len; i++) {
total += numbers[i];
}
return {
average: total / arguments.length,
max: Math.max(numbers); //spread array into formal params
}
}
stats(5, 6, 8, 5); //{average: 6, max: 8}
Hope this help you understand "..."!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With