4 ms·
What does this trick do?
by drusepth 7y ago
What does this trick do?
- grardb 7y agoWritten in older JS, it would look like this: $ = function(_) { return document.querySelector(_) } It basically turns `$` into an alias for `document.querySelector`. I'm not sure that it's any different from: $ = document.querySelector
- iudqnolq 7y agoSadly it is different because of the magic of this. If you set something equal to document.querySelector and then call it this will no longer refer to document. There are fancier ways with Function.prototype.bind, but wrapping in a function is the easiest conceptually. https://www.taniarascia.com/this-bind-call-apply-javascript/ https://www.taniarascia.com/this-bind-call-apply-javascript/ For this specific case, you can see this at https://stackoverflow.com/q/13383886/8334056 https://stackoverflow.com/q/13383886/8334056