Fab*_*ano 6 javascript document bind parallax.js
我正在检查来自html5rocks的代码:http://www.html5rocks.com/static/demos/parallax/demo-1a/scripts/parallax.js
并注意他们使用
(function(win, d) {
var $ = d.querySelector.bind(d);
....
var mainBG = $('section#content');
....
})(window, document);
Run Code Online (Sandbox Code Playgroud)
为什么他们将文档绑定到querySelector.是不是它已经作为文件的范围?
不,该函数不绑定到特定文档(可能还有其他文档,而不仅仅是window.document)。尝试不使用它,您将得到一个异常 - 您将需要将其应用到实现该接口WRONG_THIS_ERR的对象上。Document
另请参阅MDN 对this关键字的介绍,了解如何thisVal确定函数调用的(“上下文”)。