Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minify javascript with default value issue

I have functions with a default value like this:

function f(a, b = 'something') {
    //do stuff
}

This works just fine, but if I try to minify my JS file using online related apps, an error occurs :

Error: Unexpected token operator '=', expected punc ','

As I know using = to set default value in Javascript is legal, so why do I receive this error?

Do i have to define a default value in the body of function?

like image 979
alex Avatar asked Mar 27 '16 13:03

alex


People also ask

How do you minify a JavaScript file?

To minify JavaScript, try UglifyJS. The Closure Compiler is also very effective. You can create a build process that uses these tools to minify and rename the development files and save them to a production directory.

Does Minified JS run faster?

The main purpose of JavaScript minification is to speed up the downloading or transfer of the JavaScript code from the server hosting the website's JavaScript. The reason that minification makes downloads go faster is because it reduces the amount of data (in the minified JavaScript file) that needs to be downloaded.

Does Uglify minify?

Uglify JS is a JavaScript library for minifying JavaScript files.

How do I minify JavaScript in Visual Studio?

Minify any JS, CSS or HTML file by right-clicking it in Solution Explorer. That will create a [filename]. min. [ext] and nest it under the original file.


1 Answers

Using = to set a default default values for function parameters in Javascript is an ES6 feature that is currently only supported by Chrome 49 and Firefox 15.0 :

enter image description here

Because of the limited browser support, few (if any) minifiers already support this feature.

Alternative 1 :

You could set default parameters like this :

function f(a, b) {
    b = typeof b  === 'undefined' ? 'something' : b;
    //do stuff
}

Alternative 2 :

You could use a transpiler like Babel to convert ES6 code to something that older browsers & minifiers understand.

like image 193
John Slegers Avatar answered Oct 04 '22 22:10

John Slegers