Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assigning to multiple variables in one line throws error #359

Closed
timwis opened this issue Dec 8, 2015 · 3 comments

Comments

@timwis
Copy link

commented Dec 8, 2015

var $ = jQuery = require('jquery')

I use this to accommodate bootstrap.js in browserify. It throws the error "jQuery" is not defined.

Have I misunderstood what this does in JavaScript this whole time? I'm under the impression that it sets both $ and jQuery to the value of require('jquery').

@Flet

This comment has been minimized.

Copy link
Member

commented Dec 10, 2015

That is totally valid JS!

standard uses the eslint rule no-undef which disallows variables that are not explicitly defined. This helps avoid leaking global variables accidentally.

If defining jQuery as a global variable is whats intended, then just add a hint at the top of the file:

/* global jQuery */
var $ = jQuery = require('jquery')

If it should not be global, just declare it:

var jQuery
var $ = jQuery = require('jquery')

even this is valid in standard too... Maybe its more visually appealing:

var $, jQuery
$ = jQuery = require('jquery')

Its also possible to add a "standard" attribute to package.json to define globals for your project:

"standard": {
"globals":["jQuery"]
}

This would make your original code work without issue in all your files.

I'm going to close this issue for now, but feel free to comment if you have questions/concerns!

@Flet Flet closed this Dec 10, 2015

@rstacruz

This comment has been minimized.

Copy link
Member

commented Dec 10, 2015

If I may add,

var $ = jQuery = require('jquery')

is the same as:

var $ = (jQuery = require('jquery'))

which unrolls to:

jQuery = require('jquery')
var $ = jQuery

...which is what standard complains about: declaring jQuery as a global. However, this is fine:

var jQuery = require('jquery')
var $ = jQuery
/* ✓ ok */

or this:

var $, jQuery
$ = jQuery = require('jquery')
/* ✓ also ok */
@timwis

This comment has been minimized.

Copy link
Author

commented Dec 10, 2015

Thanks so much guys - I always assumed it was declaring the second var, but I totally see how it wouldn't. I appreciate the detailed explanation and options!

@lock lock bot locked as resolved and limited conversation to collaborators May 10, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
3 participants
You can’t perform that action at this time.