Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of document.querySelector.bind(document);

I was checking this code from html5rocks: http://www.html5rocks.com/static/demos/parallax/demo-1a/scripts/parallax.js

And notice that they use

(function(win, d) {

  var $ = d.querySelector.bind(d);

  ....

  var mainBG = $('section#content');

  ....

})(window, document);

Why they bind the document to the querySelector. Isn't it already scoped to the document?

like image 631
Fabrizio Giordano Avatar asked Jan 10 '13 05:01

Fabrizio Giordano


1 Answers

No, the function is not bound to a specific document (there may be other ones, not just window.document). Try it without, and you will get an WRONG_THIS_ERR exception - you will need to apply it on an object that implements the Document interface.

Also have a look on MDN's introduction to the this keyword on how the thisVal("context") of a function call is determined.

like image 137
Bergi Avatar answered Oct 12 '22 10:10

Bergi