0

I'm new to Javascript, although I have a .net background. Typically in .NET (as well as many other languages), if a method requires a parameter to be passed, you have to pass it (or else compiler error due to incorrect signature). This also appears to be the case in JavaScript, but not all cases it would appear.

This doesn't appear to be the case in Javascript.

As a working example, please refer to line 61

http://www.humblesoftware.com/flotr2/#!basic-axis

Line 61 is tickFormatter: ticksFn,

I understand that tickFormatter is calling a function called ticksFn but line 29 shows

function ticksFn(n) {
    return '(' + n + ')';
}

'ticksFn' requires a value (n) to be passed, yet, we never pass it.

Despite that, javascript still gets it right and I can't find out how, nor can I work/understand what to search for to do more research

MyDaftQuestions
  • 4,487
  • 17
  • 63
  • 120
  • 2
    Do you know that `tickFormatter: ticksFn,` does not call the method? – Tobias Mar 06 '14 at 10:46
  • 1
    Function is never called.. about line 61, `tickFormatter: ticksFn` <--- `ticksFn` is a value, not the function, function calls always have `()` – Mr. Alien Mar 06 '14 at 10:48

2 Answers2

1

You never call it at all. You pass the function itself as an argument (or rather as the value of a property of an object that is an argument).

graph = Flotr.draw(container, [ /* ... */], {
      xaxis : {
        noTicks : 7,              // Display 7 ticks.
        tickFormatter : ticksFn,  // Displays tick values between brackets.
        // …

Your third party library code is responsible for actually calling that function, and it does so with an argument.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

In javascript it is not required to pass the parameters for a function as a value of undefined is passed if you don't.

So a function:

function ticksFn(n) {
return '(' + n + ')';
}

Can be invoked:

ticksFn();

Without any problem and the value of the n will be undefined..

Alternatively a function defined with no arguments:

function ticksFn() {
return '(' + arguments[0] + ')';
}

And calling:

ticksFn(10);

arguments[0] will be 10. and accordingly you can read any number of arguments.

That was a quick tutorial about javascript functions.. now about your code.. javascript is a function oriented language and so writing the function name without parentheses actually hands reference on the function rather than calling the function itself..

  • 1
    I'm not sure if this really answers the question when we've established ticksFn is not a parameter. I'm aware if no parameter is passed then it turns to undefined in the function, but, in my question the parameter is defined... – MyDaftQuestions Mar 06 '14 at 10:52
  • Yes sorry I re-read your question and I realized I was misunderstanding the question.. I added a paragraph anyway at the end to tell a hint about your question.. sorry :-) – Mohammed R. El-Khoudary Mar 06 '14 at 10:53
  • 1
    No worries, thank you for taking the time :) Although I will point you to this http://stackoverflow.com/questions/3962604/is-javascript-a-functional-programming-language – MyDaftQuestions Mar 06 '14 at 10:55