4 ms·
>> Tries to play it cool by wrecking jQuery? > not sure exactly how he's "wrecking jQuery" The grandparent is misguided, but I believe they mean using '$' as
by nailer 11y ago
>> Tries to play it cool by wrecking jQuery?
> not sure exactly how he's "wrecking jQuery"
The grandparent is misguided, but I believe they mean using '$' as the QSA. The grandparent probably uses script tags and globals in which case there would be a conflict if a script tag library requires window.$.
JS modules should var $ = require('jquery') (or equivalent) and then use the $ in their local scope if they need jquery.
- bad_user 11y agoWhat modules? You mean CommonJS or was it AMD or ECMAScript 6? Still a straw man IMHO :-)
- nailer 11y agoCommonJS, since npm (which is mainly commonjs) is a few hundred/thousand times larger than the whatever else.
- tracker1 11y agoIIRC, there is a jQuery global object available.. you can assign it to a local variable inside in iife, which is the appropriate way to use jQuery when writing a module... (function(window,$){ //module code goes here }(this.self || this, jQuery)); In this way, you are keeping jQuery itself and your implicit scope assigned to localized variables. If you are writing a modern module (not necessarily jQuery based), then following node/cjs conventions is probably best. If you use this convention with such a plugin, said module should probably return the jQuery object that has been modified, and extra care should be taken that all plugins are using compatible/same instance of jQuery.