In jquery when I run:
$('li').each(some-function(i){});
The IDE seems to know that some-function is expecting an index.
It leads me to believe I can make a function in javascript/jquery that expects a function as one of it’s parameters that expects certain paramters. (I am new to javascript)
I know how to do it in c language.
I am looking for something like this:
void somefuncA ( void (*funcB)(int) ){
//do stuff with funcB
(*funcB)(5);
}
So the function ‘somefuncA’ expects to get a function funcB that gets one integer as a parameter and then does something.
Hope that makes sense.. thanks
p.s. I know JavaScript is weakly type but I also noticed that .each() expects a function as parameter and not just any function but one with an int as parameter.
You may be talking about delegates here. Functions are first-class objects in JavaScript. They don’t define a type-safe pointer like in some other languages. A function can define as many parameters as it likes, but you don’t have to pass even a single one. Skipped parameters will be undefined. They also don’t define any return type. A function may or may not return a value.
Your IDE is clever enough to find the function call and give you a hint which parameters are provided. You are free to use or skip these parameters in your function.
Here you create an anonymous function that does not take any parameters. jQuery will pass index or some other parameters inside but it will not produce any errors. You can also create a function that uses the index parameter provided by jQuery.