Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$ Variable in Chrome?

I was working with the developer tools of google chrome on a page without jQuery (or any other library that uses the $ sign as a shortcut). When I inspected $ by the console (by just typing it in and hitting enter), i got this:

$ function () { [native code] } 

So, chrome has some native function that can be referenced by $. Only chrome seems to have this one and i cannot access it via window['$'] nor via document['$'] or this['$'].

I was not able to find out what this function is. Do you know what it does and maybe have some background information on this? Thanks in advance!

like image 789
Dennis Avatar asked Aug 02 '12 13:08

Dennis


People also ask

How do I view variables in Chrome?

To view any variable in chrome, go to "Sources", and then "Watch" and add it. If you add the "window" variable here then you can expand it and explore.

How do you show variables in inspect element?

To inspect local variable values While running in Debug mode, double-click any variable that appears in the Local Variables window. This displays the Debug Inspector for that local variable. Inspect the variable's value. Change the value by clicking the button with an ellipsis (...) on it.

How do I view variables in dev tools?

To enable this feature (if it's not already): Go to DevTools settings (the cog wheel). Check General > Sources > Display variable values inline while debugging.


1 Answers

This has changed yet again, even since just last year.

The devtools console provides $ as an alias to document.querySelector, along with many other things; here's an excerpted list:

  • $(selector) returns the reference to the first DOM element with the specified CSS selector. This function is an alias for the document.querySelector() function.
  • $$(selector) returns an array of elements that match the given CSS selector. This command is equivalent to calling document.querySelectorAll().
  • $_ returns the value of the most recently evaluated expression.
  • The $0, $1, $2, $3 and $4 commands work as a historical reference to the last five DOM elements inspected within the Elements panel or the last five JavaScript heap objects selected in the Profiles panel.

...and a bunch of others.

Note how it calls $ an alias of document.querySelector, but says $$ is "equivalent" to calling document.querySelectorAll. Neither seems to be literally true; $ === document.querySelector is false, and $$ returns an array, not a NodeList.

like image 84
T.J. Crowder Avatar answered Oct 01 '22 09:10

T.J. Crowder