For funsies, I tried to create a shorthand version of document.querySelectorAll by making it usable like the way jQuery does it. Here is the code:
window.$ = document.querySelectorAll;
var links;
links = document.querySelectorAll('a'); // Works
links = $('a'); // Doesn't work
For some reason, $
isn't working even though it is a reference to document.querySelectorAll
. I know that I could do this instead:
function $(selector) {
return document.querySelectorAll(selector);
}
but I just don't understand why the first example doesn't work.