1

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.

Josh Mein
  • 28,107
  • 15
  • 76
  • 87
AturSams
  • 7,568
  • 18
  • 64
  • 98
  • Please explain your goal. You can't declare types in JavaScript if that's what you are asking. – Shadow The GPT Wizard Jun 20 '12 at 11:15
  • let us know what exactly you want to do. – Subhajit Jun 20 '12 at 11:16
  • 1
    *and not just any function but one with an int as parameter.* here you're wrong. `each` does not expect this kind of a function, it will just provide the index if the function accepts it. If you pass a function that doesn't, it's still no problem. – Yoshi Jun 20 '12 at 11:19
  • Is there any way to tell people or the IDE that if you do give a function that expects an index, it will work? Meaning that it will use the index if it is available. – AturSams Jun 21 '12 at 21:08

3 Answers3

1

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.

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.

$('li').each(function(){
   // do something
});

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.

$('li').each(function(index){
   // do something
});
Community
  • 1
  • 1
Ufuk Hacıoğulları
  • 37,978
  • 12
  • 114
  • 156
0

what you are asking is something like this?

function somefuncA ( funct, parameters ){
    //do stuff with funcB
    alert(funct(parameters));
}

This is mostly used for callbacks in javascript.

KoU_warch
  • 2,160
  • 1
  • 25
  • 46
0
function ​myFunc​(fn, param)
{
    if (typeof fn != "function") {
        throw new TypeError();
    }
    fn.call(this, param);
}

Call myFucn with two parameters, first param is function and second param is it's parameter 'Hello'

myFunc(function(m){
    alert(m);
}, 'Hello!');​

DEMO.

The Alpha
  • 143,660
  • 29
  • 287
  • 307