Pass in jQuery's $ as a reference#2
Open
kingkool68 wants to merge 1 commit intojessabean:masterfrom
Open
Conversation
Most of the time this is fine `$( document ).ready(function() {` but sometimes jQuery is loaded in noConflict mode which means the shorthand `$` is not mapped to the jQuery object. We can get the best of both worlds to maximize compatibility and convenience.
`jQuery(document)` will always work for selecting the document so long as jQuery is already included before this script is executed. We can pass a `$` as an argument to the anonymous function that is being called when `ready()` fires. Thus everything within the function can use the shorthand `$` instead of typing out `jQuery` everywhere.
You can also move all of the functions like `convertToHex()` and `rgbToHex()` inside of the `jQuery( document ).ready(function($) {` they will still work and better yet will be protected from function name collisions in the global scope. In other words, there could be another function named `convertToHex` used on the page and there won't be an error because one will be global and yours will be within its own scope. The downside to this is you can't call it outside of the context of your anonymous function which in reality means you can't open up dev tools, go to the console and type `rgbToHex()` and expect it to work.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Most of the time this is fine
$( document ).ready(function() {but sometimes jQuery is loaded in noConflict mode which means the shorthand$is not mapped to the jQuery object. We can get the best of both worlds to maximize compatibility and convenience.jQuery(document)will always work for selecting the document so long as jQuery is already included before this script is executed. We can pass a$as an argument to the anonymous function that is being called whenready()fires. Thus everything within the function can use the shorthand$instead of typing outjQueryeverywhere.You can also move all of the functions like
convertToHex()andrgbToHex()inside of thejQuery( document ).ready(function($) {they will still work and better yet will be protected from function name collisions in the global scope. In other words, there could be another function namedconvertToHexused on the page and there won't be an error because one will be global and yours will be within its own scope. The downside to this is you can't call it outside of the context of your anonymous function which in reality means you can't open up dev tools, go to the console and typergbToHex()and expect it to work.