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?
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.
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.
Uglify JS is a JavaScript library for minifying JavaScript files.
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.
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 :
Because of the limited browser support, few (if any) minifiers already support this feature.
You could set default parameters like this :
function f(a, b) {
b = typeof b === 'undefined' ? 'something' : b;
//do stuff
}
You could use a transpiler like Babel to convert ES6 code to something that older browsers & minifiers 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