简体   繁体   中英

How do certain methods know to take an argument in advance?

I am a bit confused by the fact that I can sometimes use functions in certain ways without actually declaring that an argument must be passed inside it.

Here are some examples, I know that they are different in terms of how they might be interpreted but the overall theory seems a bit strange to me.

So normally you would pass the argument with the variable declaration like so:

def usualexample(x):
        print x;
        return False;


somevarname = usualexample(5);
somevarname;

Which would print out in this case 5 and return False. But in some cases like in this example with JavaScript I can use the function without passing any argument at all even though the program will know to pass an event as an argument into the function without the programmer actually telling it to like the following.

var innermost = document.getElementsByTagName("section");


for (var p = 0; p < innermost.length;p++){
    innermost[p].onclick = showinfo;

}




function showinfo(event){
    Do something with the event argument
}

Another similar example in Python with the pyhook module would be:

import pyHook;
def dosomething(event):
    do something with the event argument.
manager = pyHook.HookManager();
manager.KeyDown = dosomething;

in case of the innermost[p].onclick and in case of the manager.KeyDown declarations how do they know in advance to supply the function with an argument?

Could anyone please explain the logic behind this? I know they are separate declarations and fill a complete different purpose but I just need to know how these declarations know in advance to supply the function with the argument without actually specifying it to them in advance.

The code was designed to pass in an argument. Whatever handles events in JS and pyHook will always pass in an event object to a handler. There is nothing magical about this.

In JS, it is fine for a function to ignore arguments. So the following is legal:

function foo() { ... }
foo('bar');

This is not the case in Python however; if you assign a function that takes 0 arguments to a hook it'll throw an exception.

There is a difference to executing the code of a function and just storing a reference to the function without executing it. When you write the function:

function showinfo(event){
  console.log('showinfo executed');
}

nothing is executed. You just define what the function should do when it is executed. You can now call it immediately by using parentheses.

showinfo(3);

This will cause the message to show in the console. However you can also pass a reference (kind of like the name) of a method. So if you write:

var innermost = document.getElementsByTagName("section");
innermost[0].onclick = showinfo;

Nothing happens, but internally the browser knows to call the 'showinfo' method when a click occurs. You can do this in your own code too:

var rememberedFunction;
function rememberFunction(func) {
  rememberedFunction = func;
} 
function callFunction(value) {
  if (rememberedFunction) {
    rememberedFunction(value); // This calls the function you saved
  }
}

rememberFunction(someinfo); // Nothing is output to the console

// ... much later
callFunction(3);            // The message is output to the console here

You can do this to post pone the execution of methods in all kinds of situations from browser events to handling ajax calls and promises.

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.

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