Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Questions about function signature

Tags:

javascript

I found a javascript example online that made me a bit confused. I’m quite new to javascript but have good knowledge in “traditional” languages. I was not able to find an answer when I tried google for it so I’ll ask the question here and hope someone can help me.

From one "class" the following code was executed:

this.foo.addListener("xType", this, this.boo);

The function that were called looked like this:

//first argument (type:String) what kind of event
//second argument (type:Function) listener - listening function
addListener: function(kindOf, listener) {

What I don't understand is that the number of arguments does not match. When the function is called 3 arguments are used namely "xType", this and this.boo but in the function signature there is only 2 arguments namely kindOf and listener. Is this some javascript functionality that you can call functions with some other amount of arguments than what is declared in the function? Or how is this code supposed to work?

like image 829
Kneta_ Avatar asked Jul 31 '11 21:07

Kneta_


People also ask

What is included in function signature?

Function Signature A function's signature includes the function's name and the number, order and type of its formal parameters. Two overloaded functions must not have the same signature. The return value is not part of a function's signature.

Why is function signature important?

Function signatures provide a much more powerful and specific way to think about function usage and types. We can write signatures for functions that take a fixed number of arguments: circle : number, string, string -> image.

What are the two parts of a function signature?

A function signature includes the function name, its arguments, and in some languages, the return type.

What is the role of a function signature in disambiguation process?

Explanation: In the programming language, Java, a method signature is the procedure that specify name and the number, type and order of its parameters. Return types and thrown exceptions are necessarily considered to be a part of the method signature. Disambiguation express to the remove ambiguity by making it clear.


1 Answers

Is this some javascript functionality that you can call functions with some other amount of arguments than what is declared in the function?

This is correct. JavaScript does not require that you call a function with the same number of arguments as was used to define it.

If you call it with too few, the missing values will have the special value undefined. If you call it with too many, the function will need to use the special array value arguments to get at them. For example,

function alertMany() {
    for (var i = 0; i < arguments.length; i++) {
        alert(arguments[i]);
    }
}

alertMany("hello", "goodbye");
alertMany("hello", "hello again", "hello once more", "farewell");
alertMany()

All of these calls will work, displaying up one alert box for each argument.

like image 165
Jeremy Avatar answered Oct 04 '22 08:10

Jeremy