Reputation: 40618
I am creating a class that enables me to select any element by selector and quickly apply JS methods on it. I have a small issue with the code bellow (which is the first part of my code). When I compress it with Koala or on jscompress.com I get an error message:
SyntaxError: Name expected (line: 31, col: 31)
The line corresponds to the wrapper method starting with $.prototype.method
. I don't understand what's wrong since my code is perfectly working both in local and in live website.
Somehow there's an error there. I tried changing the variable name
to another one but it gave me the same error.
var $ = function(element) {
if(!(this instanceof $)) {
return new $(element);
}
// select all elements with this identifier
this.elements = document.querySelectorAll(element);
this.length = this.elements.length;
if(this.length == 0) {
return this;
}
// by default select the first element of querySelectorAll
this.element = this.elements[0];
this.css = this.element.style;
// first method applied will be exectuted directly
this.delayTime = 0;
}
// add a time to the delay timer
$.prototype.delay = function(delayTime) {
// set a delay for the following method applied
this.delayTime += delayTime;
return this;
}
// wraps the method into a setTimeout
$.prototype.method = function(name, fn) {
$.prototype[name] = function(...args) {
var that = this;
if(this.length != 0) {
setTimeout(function() {
// only one relevant param is passed
fn(that, args[0]);
}, this.delayTime);
return this;
}
return 'element does not exist!';
};
}
Thanks in advance!
Upvotes: 0
Views: 2250
Reputation: 536
The error you are getting is referring to the spread operator – ...args
.
The spread operator is relatively new and probably isn't supported by your minifier.
To fix the error, remove ...args
from the arguments list on line 31. On line 36, use the arguments
object instead – fn(that, arguments[0]);
Upvotes: 1
Reputation: 280237
The error message is pointing to this line:
$.prototype[name] = function(...args) {
because the minifier you're using doesn't understand ECMAScript 6 spread syntax.
Upvotes: 3