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
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:
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? 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. 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. So, arguments[arguments.length-1]
will always be the last parameter sent to the function.
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.
Hope it's clear.
Cheers, from La Paz, Bolivia
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.