call.call 在 javascript 中完成了什么
在 Modernizr 源代码中找到了这段摘录。
var documentCreateElement = scopeDocument.createElement, documentCreateDocumentFragment = scopeDocument.createDocumentFragment;
// shiv the document
for (var i = 0, elements = html5.elements, l = elements.length; i < l; ++i) {
call.call(documentCreateElement, scopeDocument, elements[i]);
}
// shiv the document create element method
scopeDocument.createElement = function (nodeName) {
var element = call.call(documentCreateElement, scopeDocument, nodeName);
我想知道为什么需要使用 call.call
,而不是仅仅使用 call
documentCreateElement.call(scopeDocument,nodeName)
的作用是什么?代码> 不是吗?
提前致谢
Found this excerpt in the Modernizr source code.
var documentCreateElement = scopeDocument.createElement, documentCreateDocumentFragment = scopeDocument.createDocumentFragment;
// shiv the document
for (var i = 0, elements = html5.elements, l = elements.length; i < l; ++i) {
call.call(documentCreateElement, scopeDocument, elements[i]);
}
// shiv the document create element method
scopeDocument.createElement = function (nodeName) {
var element = call.call(documentCreateElement, scopeDocument, nodeName);
I was wondering why it was necessary to use call.call
, as opposed to just call
What is the accomplishing that documentCreateElement.call(scopeDocument,nodeName)
does not?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
call.call
使用不同的上下文调用用户定义的函数call
。call
是一个原生 JavaScript 函数。这是一个可以在函数上调用的函数,因为在 JavaScript 中函数是一等公民,它被称为call
,这很令人困惑:Pcall
是上下文,无论this
在被调用函数中应该引用什么。这是一个例子:所以
documentCreateElement.call(scopeDocument,nodeName)
基本上是documentCreateElement(nodeName)
但this
在documentCreateElement< /code> 指向
scopeDocument
。您可能想知道您发布的示例代码是否很好地使用了call
。如果用错了,我总觉得很复杂,不合时宜~_~call.call
calls the user defined functioncall
with a different context.call
is a native JavaScript function. It's a function you can call on a function, because in JavaScript functions are first class citizens, and it's calledcall
, which is very confusing :PThe first parameter of
call
is the context, whateverthis
should refer to in the called function. Here is an example:So
documentCreateElement.call(scopeDocument,nodeName)
basically doesdocumentCreateElement(nodeName)
but thethis
indocumentCreateElement
points toscopeDocument
. You can wonder whether the example code you posted is good use ofcall
. I always find it very complicated if used wrongly and out of place ~_~是的,它从
documentCreateElement
函数的上下文中调用.call
函数。所以最终它是一样的......
我假设在某个地方有对 Function.prototype.call 的引用,比如
他们可能缓存了 call 方法以防万一它会被
Function.prototype
覆盖。编辑:
As @ruakh 在下面指出,如果
Function.prototype.call
被覆盖,则 <code>call. call 不起作用,因为它也依赖于Function.prototype
。documentCreateElement
是对document.createElement
方法的引用,并且该方法是宿主对象,因此不能保证它将包含Function.prototype
> 在其原型链中。在这些情况下,这将允许他们在主机对象上使用
.call
。Yes, it calls the
.call
function from the context of thedocumentCreateElement
function.So ultimately it's the same as...
I assume that there's a reference to
Function.prototype.call
somewhere, likeThey probably cache thecall
method just in case it gets overwritten onFunction.prototype
.EDIT:
As @ruakh pointed out below, if
Function.prototype.call
was overwritten, thencall.call
wouldn't work since it also relies onFunction.prototype
.documentCreateElement
is a reference to thedocument.createElement
method, and that method is a host object, so there's no guarantee that it will includeFunction.prototype
in its prototype chain.This would allow them to use
.call
on host objects in those cases.