0

I used JSLint on a zepto extension js file

It threw the error:

Unexpected '='. line 33 character 37

line 33:

return (document.cookie = [encodeURIComponent(key), '=', options.raw ? value : encodeURIComponent(value), options.expires ? '; expires=' + options.expires.toUTCString() : '', options.path ? '; path=' + options.path : '', options.domain ? '; domain=' + options.domain : '', options.secure ? '; secure' : ''].join(''));

What does this mean?

dswwsd
  • 133
  • 5

1 Answers1

4

Split it to 2 statements:

document.cookie = [encodeURIComponent(key), '=', options.raw ? value : encodeURIComponent(value), options.expires ? '; expires=' + options.expires.toUTCString() : '', options.path ? '; path=' + options.path : '', options.domain ? '; domain=' + options.domain : '', options.secure ? '; secure' : ''].join('');
return document.cookie;

generally you don't use assignment in a return expression.

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • 1
    @zerkms.. Why would that be a problem.. ?? The value is first assigned and then returned right – Sushanth -- Nov 01 '12 at 03:23
  • 1
    @Sushanth --: a problem from what perspective? JSLint hints you about *potential problems* and *bad practices*. In this case it may be either: problem of confusing `=` and `==`, or just mis-using assignment – zerkms Nov 01 '12 at 03:24
  • I know that's a bad practice but was wondering if this is just in perspective of JSLint .. Cause in general the correct value will be return by the function right – Sushanth -- Nov 01 '12 at 03:27
  • 2
    @Sushanth --: Well, JSLint has even more strange checks like: http://stackoverflow.com/q/3000276/251311 But in this case I'm 100% agree with JSLint, I think it's just an example of bad coding practice – zerkms Nov 01 '12 at 03:29