简体   繁体   中英

Javascript/jQuery function arguments

Newbie jQuery / Javascript question here

I see functions written in the following fashion:

some_function = function(event) {

}

My question is: What is meant by that event argument? Is it optional? What if I want to have two additional parameters, X and Y, what would the signature look like? When I call the function now, I can just call it like some_function(); and it works fine (which leads me to believe that it's optional). However, how will that change when I have two additional arguments like X and Y? Can I just call it like some_function(myX, myY) ?

Thanks!

There are two ways to instantiate a function in JavaScript. They both look the same, but their meanings aren't quite the same.

What you've posted is a function instantiation as part of an expression in the language. In that form, the syntax is

function name ( p1 , p2 , ... ) body 主体

The "name" and the parameters are optional; the keyword function and the parentheses are required. (There are some obscure issues with using a name in this case, in some browsers; it's getting to be less of a problem.)

The effect of that is to create a new function object. The reference to the object participates in the expression just like any other value (well, like any other reference to an object). Because it's a function, the reference can also be used to call the function and cause its code ("body") to execute, just like you'd expect. (It wouldn't be much of a function if you couldn't!)

The other way a function can be instantiated is with a function declaration statement, which looks, surprisingly, exactly the same (except that "name" is required). The difference involves where exactly in your code the keyword function appears:

  • If the keyword function is the first thing in a new statement, then you've got a function declaration and the "name" is required. The statement is not an expression statement in this case, and the only thing the statement does is instantiate the function and bind a reference to the function to "name" more or less as if "name" were a local variable (or global if in the global scope).

  • If the keyword function appears anywhere else in an expression (either an expression statement, or an expression buried inside some other context, like the top of a for or while loop statement), then it's a function instantiation expression.

Now, regardless of how a function is "born", once you've got a reference to the function you can call it and pass as many parameters as you like.


I don't know personally how the trend started, or whether it's even something that should be considered a trend, but it's fairly common to see local functions instantiated with code like this (and like what you posted):

var some_function = function( arg1, arg2 ) {
  /* some code */
};

That's an example of a function instantiation in an expression. The net effect is that the symbol "some_function" is bound to the newly-created function. There are slight nitpicky differences, however, between the way that name is bound to the function from the (almost) equivalent function declaration:

function some_function( arg1, arg2 ) {
  /* some code */
};

One simple reason that the second way (function declaration statement) is a little better is that the name of the function will show up in stack traces. Of course, one could achieve that with the redundant-looking:

var some_function = function some_function( arg1, arg2 ) {
  /* some function */
};

I don't really know why you'd want to do that, but it'd work, except in some naughty environments .

That code snippet is a little vague, however I can answer, in general, your questions.

The event argument, in the code you provided, is just what that function will use to reference the first parameter that is passed to the function from whatever-other code calls it. For example, if you had code that called your some_function function, and passed it a string "hello world!" , the call would look something like:

obj.some_function("hello world!")

Inside of the some_function function, the variable event would contain "hello world!" . Also, you could change your some_function signature to be: some_function(blah) and it would be all the same, you would just use blah to reference the parameter instead of event . The variable name you choose for parameters is entirely up to you, though you want to make sure you don't use language-reserved names (like in , for , continue , break , etc)

Technically all parameters are optional for a JavaScript function, unless the internal code of the function enforces the parameters (ie it may return or throw an error if a parameter is missing.

@Pointy answered the other point I was going to make...that the code you provided is defining that function as an expression. I use that syntax when I'm creating a function that is an attribute of an object (which is why my above code has obj. at the beginning.

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