Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript document.querySelector.bind(文档)的含义是什么;_Javascript_Bind_Document_Parallax.js - Fatal编程技术网

Javascript document.querySelector.bind(文档)的含义是什么;

Javascript document.querySelector.bind(文档)的含义是什么;,javascript,bind,document,parallax.js,Javascript,Bind,Document,Parallax.js,我在检查html5rocks中的代码: 请注意,它们使用 (function(win, d) { var $ = d.querySelector.bind(d); .... var mainBG = $('section#content'); .... })(window, document); 为什么要将文档绑定到querySelector。它不是已经限定到文档的范围了吗?否,没有绑定到特定文档(可能还有其他文档,而不仅仅是window.document)。在不使

我在检查html5rocks中的代码:

请注意,它们使用

(function(win, d) {

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

  ....

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

  ....

})(window, document);
为什么要将文档绑定到querySelector。它不是已经限定到文档的范围了吗?

否,没有绑定到特定文档(可能还有其他文档,而不仅仅是
window.document
)。在不使用的情况下尝试它,您将得到一个
错误\u此错误
异常-您需要将其应用于实现的对象


还可以了解函数调用的
thisVal
(“上下文”)是如何确定的。

对于未来的谷歌用户,作为补充说明,您还可以使用
document.querySelector.bind(document)
进行类似jQuery的选择:

var $$ = document.querySelector.bind(document);
console.log( $$('#answers').textContent );
var$$=document.querySelector.bind(文档);

log('yeolde StackSnippet body:',$$('body').textContent)谢谢。假设我们没有在浏览器中处于“严格模式”,我们创建了一个函数:
functiona(){console.log(this);}
如果我们调用
a()控制台打印
窗口
。但是它就像javascript prepend
this。
幕后
this.a()这是窗口上下文。出于同样的原因,如果我们创建一个新对象
varo={}并且我们分配a方法
o.a=ao.a()控制台打印o对象。我知道这可能不是正确的解释,但javascript中的“上下文”有时让我发疯。有进一步的解释。简短回答:JavaScript解释器引发错误,因为querySelectorAll()应该在文档上下文中调用。