1
var Page = {
    data: null,
    Load: function () {
        this.Populate;
    },
    Populate: function () {
    }
};

$(document).ready(Page.Load);
  1. Why can't I reference Page.Load as a function in ready() eg .ready(Page.Load())
  2. Why can't I call this.Populate() from the Load function, I just get this.Populate() is not a function.
2
  • 2
    You're not making our lives any easier with a confusing title. Commented Sep 27, 2011 at 10:33
  • 1
    @BoltClock Yo dawg! We heard you like queries so we put a query in your jQuery so you can query while jQuery-ing Commented Sep 27, 2011 at 10:37

5 Answers 5

4

Why can't I reference Page.Load as a function in ready() eg .ready(Page.Load())

Sticking () on the end calls a function. You want to pass the function itself, not its return value. (The ready function will call what you pass to it later).

Why can't I call this.Populate() from the Load function, I just get this.Populate() is not a function

Two reasons.

First, you never actually call the Populate function. Change to:

this.Populate()

Second: because you detach the Load function from the object, so this isn't Page by the time it gets called.

Do this instead:

$(document).ready(function () { Page.Load() });
Sign up to request clarification or add additional context in comments.

Comments

3

There are two problems in your code.

First, as Rob says, you're not actually calling the Populate() method in Load(). You need to add parenthesis to do that.

Second, the this keyword in Populate() does not refer to the Page object. You can use $.proxy() to set the Page object as the context of the method call:

var Page = {
    data: null,
    Load: function() {
        this.Populate();
    },
    Populate: function() {
    }
};

$(document).ready($.proxy(Page.Load, Page));

2 Comments

Wow! All answers really useful! I love Stack Overflow.
went for this answer as it seems more jquery and is smaller in code :)
3

A function call should be in the format function_name() (parentheses!). A function reference should be function_name (without parentheses). This code will work:

var Page = {
    data: null,
    Load: function () {
        Page.Populate(); //PARENTHESES
    },
    Populate: function () {
    }
};

$(document).ready(Page.Load);//NO PARENTHESES

$(document).ready is a function which expects a function to be parameter 1. If you use Page.Load(), you will execute function Page.Load() and pass the return value (undefined, in this case) to $(document).ready.

3 Comments

this.Populate(); will not work - this is set by jQuery to be the window.
Thanks for your answer but I forgot to mention that this.Populate works but this.Populate() doesn't. I have updated my question. Sorry about that
@Kobi Thanls for mentioning. this.Populate doesn't work, because .ready(Page.load) executes the function argument within the scope of JQuery.
2

You can get it to work by wrapping your object up in a function like this:

var Page = (function () {
    var that = {
        data: null,
        Load: function () {
            that.Populate(); 
        },
        Populate: function () {
            alert("Test");
        }
    };
    return that;
})(); // Execute the function immediately to return 'that'

$(document).ready(Page.Load);

This way you can call between methods as you have a reference to the object (that).

JSBin Example

2 Comments

Thanks Richard, Im going to use Frederic's answer with the proxy.
np, feel free to upvote mine though because it's obviously awesome! ;)
0

You're calling the function Page.Load. But more specifically you're calling the function that Page.Load is set to (since that argument to .ready() is just a simple value); you're not calling it on Page, so 'this' is not reset to point to the Page object.

This will work better:

$(document).ready(function() {
    Page.Load();
});

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.