简体   繁体   English

为什么用JavaScript中的arguments [arguments.length-1]表示回调函数?

[英]Why are Callback functions represented by arguments[arguments.length-1] in javascript?

I had copied a piece of code from somehwere which basically was used to extend the JQuery's hide method and extend it to call a callback 我已经从somehwere复制了一段代码,该代码基本上用于扩展JQuery的hide方法并扩展为调用回调

But am not clear with how the method works .. 但尚不清楚该方法如何工作..

       js.executeScript("window.webdrivercallback = function(){};" +

            "var _oldhide = $.fn.hide;" +
            "$.fn.hide = function(speed, callback) {" +
            "    var retThis = _oldhide.apply(this,arguments);" +
            "    window.webdrivercallback.apply();" +
            "    return retThis;" +
            "};"
    );

additionally in selenium webdriver code we have to add a piece of code something like: 另外,在Selenium Webdriver代码中,我们必须添加一段代码,例如:

js.executeAsyncScript("window.webdrivercallback = arguments[arguments.length - 1];");

I am unable to understand how can we create a javascript function webdrivercallback by just passing arguments like arguments[arguments.length-1] .What is the significance of this statement? 我无法理解如何仅通过传递诸如arguments[arguments.length-1]类的arguments[arguments.length-1]来创建javascript函数webdrivercallback。此语句的意义是什么? What is arguments and from where are values being passed to the arguments field as I couldn't find a single place from where values were passed to arguments field. 什么是arguments ,值是从哪里传递到arguments字段的,因为我找不到从哪里将值传递到参数字段的单个地方。 Could someone help me understand this piece of code. 有人可以帮助我理解这段代码。

In this case, the last argument is a function, arguments is an array (well, very similar to an array) that contains all the parameters sent to the function. 在这种情况下,最后一个参数是一个函数, arguments是一个数组(很好,非常类似于数组),其中包含发送给该函数的所有参数。 So, arguments[arguments.length-1] will always be the last parameter sent to the function. 因此, arguments[arguments.length-1]将始终是发送给函数的最后一个参数。

For example, consider you have: 例如,假设您有:

function someFunction(param1, param2, param3){
    //arguments would have here: [param1, param2, param3]
    //so arguments[arguments.length-1] is the last param: param3
    var callback = arguments[arguments.length-1];   //This is what looks strange to you
    alert(typeof callback); //alerts 'function' if last param is well.. a function;
    callback();  //Execute the callback as a proof!   
}

so, let's call it: 因此,我们称之为:

someFunction('a', 'b', function(){
    alert("callback executed!!");
});

As you can see, it alerts "function", because the last parameter was a function. 如您所见,它会警告“功能”,因为最后一个参数是功能。 So, it can be assigned as a callback without problems. 因此,可以毫无问题地将其分配为回调。

Also, don't forget that we can call a function with more or less parameters than it was defined, so arguments[arguments.length-1] is a trick for always getting the last one. 另外,不要忘记我们可以使用一个比定义的参数多或少的参数来调用函数,因此arguments[arguments.length-1]是始终获取最后一个的技巧。

Hope it's clear. 希望清楚。

Cheers, from La Paz, Bolivia 来自玻利维亚拉巴斯的欢呼声

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM