1

When i was reading some code of jquery then i found this in one of their widgets

    option: {
        _page: this._getPage,
        _panelInner: this._getPanelInner()
    },
    _getPage : function(){ //code goes here that returns something..},
    _getPanelInner : function(){ //code goes here that returns something..}

I wanna know how's the first this._getPage function is being called without parenthesis. And if functions can be called like this then why the next function _getPanelInner is being called with parenthesis..?

2
  • 3
    It's not called, a reference to the function is passed instead. var a = function() {}; var b = a; <--- here 2 variables refer to the same function. Commented Apr 2, 2015 at 8:34
  • thanks a lot..It totally solved my problem.. Commented Apr 2, 2015 at 8:37

2 Answers 2

3

_panelInner will hold the value returned by the _getPanelInner function while _page will hold a reference to the _getPage function. This means that the function would be able to be called in one of the following ways:

  • option._page()
  • this._getPage()

Both of these function calls would execute the same function but that function is not called automatically (according to the code that is displayed).

Sign up to request clarification or add additional context in comments.

4 Comments

"return value of the _getPanelInner function" --- this does not sound right.
I mean the "return value" of the function, @zerkms. I'm not sure how else to phrase that...
I agree it's not easy to phrase it concisely though :-)
@zerkms '_getPanelInner' is returning an object so i think '_panelInner' is holding that object
3

It's not getting called, it's only having a reference to the function so later you can do:

option._page();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.