Saturday, October 28, 2017

what's use of "$" in Javascript

I found this interesting story about "$". Hence sharing this information as separate blog.
There is nothing inherently special about '$'. It is a variable name just like any other. In earlier days, people used to write code using document.getElementById. Because JavaScript is case-sensitive, it was normal to make mistake while writing document.getElementById. Should I capital 'b' of 'by'? Should I capital 'i' of Id? You get the drift. Because functions are first class citizens in JavaScript, you can always do this
var $ = document.getElementById; //freedom from document.getElementById!
[EDIT: Looks like in Firefox 3 and Google Chrome, you can't make alias so easily. In IE6 and Firefox2, above technique still works.]
When Prototype library arrived, they named their function, which gets the DOM elements, as '$' to save on typing/readability [When writing JS code, most of the time you start with selecting some DOM elements]. Almost all the JavaScript libraries copied this idea. Prototype also introduced $$ function to select elements using CSS selector.
jQuery not only adapted the '$ function', but expanded to make it accept all kind of 'selectors' to get the elements you want. Now, if you are already using Prototype in your project and wanted to include jQuery, you will be in problem as '$' could either refer to Prototype's implementation OR jQuery's implementation. That's why jQuery has the option of noConflict so that you can include jQuery in your project which uses Prototype and slowly migrate your code. I think this was a brilliant move on John's part! :)

Technical Writing in field of research

Research reports A research report is a collection of contextual data, gathered through organized research, that provides new insights into ...