call.call 在 javascript 中完成了什么

发布于 2025-01-03 15:04:42 字数 691 浏览 5 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风月客 2025-01-10 15:04:42

call.call 使用不同的上下文调用用户定义的函数call

call 是一个原生 JavaScript 函数。这是一个可以在函数上调用的函数,因为在 JavaScript 中函数是一等公民,它被称为call,这很令人困惑:P

call上下文,无论this 在被调用函数中应该引用什么。这是一个例子:

function doit() {
    console.log(this.myvalue);
}

function callit(context) {
    doit.call(context);
}

callit({ "myvalue": "a value"});    // a value
var obj = {
    "stuff" : "more stuff",
    "myvalue": "some value"
};
callit(obj);    // some value

所以 documentCreateElement.call(scopeDocument,nodeName) 基本上是 documentCreateElement(nodeName)thisdocumentCreateElement< /code> 指向 scopeDocument。您可能想知道您发布的示例代码是否很好地使用了 call。如果用错了,我总觉得很复杂,不合时宜~_~

call.call calls the user defined function call 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 called call, which is very confusing :P

The first parameter of call is the context, whatever this should refer to in the called function. Here is an example:

function doit() {
    console.log(this.myvalue);
}

function callit(context) {
    doit.call(context);
}

callit({ "myvalue": "a value"});    // a value
var obj = {
    "stuff" : "more stuff",
    "myvalue": "some value"
};
callit(obj);    // some value

So documentCreateElement.call(scopeDocument,nodeName) basically does documentCreateElement(nodeName) but the this in documentCreateElement points to scopeDocument. You can wonder whether the example code you posted is good use of call. I always find it very complicated if used wrongly and out of place ~_~

日暮斜阳 2025-01-10 15:04:42

是的,它从 documentCreateElement 函数的上下文中调用 .call 函数。

所以最终它是一样的......

documentCreateElement.call(scopeDocument, nodeName);

我假设在某个地方有对 Function.prototype.call 的引用,比如

var 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 the documentCreateElement function.

So ultimately it's the same as...

documentCreateElement.call(scopeDocument, nodeName);

I assume that there's a reference to Function.prototype.call somewhere, like

var call = Function.prototype.call

They probably cache the call method just in case it gets overwritten on Function.prototype.


EDIT:

As @ruakh pointed out below, if Function.prototype.call was overwritten, then call.call wouldn't work since it also relies on Function.prototype.

documentCreateElement is a reference to the document.createElement method, and that method is a host object, so there's no guarantee that it will include Function.prototype in its prototype chain.

This would allow them to use .call on host objects in those cases.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文