Skip to content

Instantly share code, notes, and snippets.

@AllThingsSmitty
Last active August 20, 2022 13:32
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save AllThingsSmitty/f9029e6236a7c2a03203 to your computer and use it in GitHub Desktop.
Save AllThingsSmitty/f9029e6236a7c2a03203 to your computer and use it in GitHub Desktop.
Use querySelector with .bind() as a shortcut to familiar function names
// returns first element selected - $('input[name="food"]')
var $ = document.querySelector.bind(document);
// return array of selected elements - $$('img.dog')
var $$ = document.querySelectorAll.bind(document);
// Credit: https://twitter.com/wesbos/status/608341616173182977
@jorgevilaca82
Copy link

Why binding? why not just pass the function reference? Like this:

var $ = document.querySelector;

@eduard-ishmukhametov
Copy link

jQuery analogy may be misleading

@AllThingsSmitty
Copy link
Author

bling.js may have the best answer for this use case.

@wesbos
Copy link

wesbos commented Aug 25, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment