0

The following code is from a simple page which displays a paginated table.

var exviewmodel = function() {
    var self = this;
    self.getList = function(pagenum) {
        $.getJSON("/api/v1/getList", { page: pagenum }, function(data) {
           self.paginator.count(data.count);
        });
    };
    self.paginator = new Paginator(self.getList);
};

I checked and verified that the code works and the count gets updated correctly in the paginator on ajax success.

I find this strange as the paginator object is being initialized only after the ajax callback has been defined.

What exactly does the JS interpreter do when it sees a function definition like getList which refers to properties which have not yet been initialized inside a async callback?

1 Answer 1

1

What exactly does the JS interpreter do when it sees a function definition like getList which refers to properties which have not yet been initialized inside a async callback?

It goes. "This is a function, OK".

Only when the function is called do the variables it tries to access matter.

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

1 Comment

Ok. So execution goes like 1. JS interpreter creates an object to hold the function definition without actually executing the code within the function. 2. self.paginator gets initialized 3. on success callback it retrieves the now initialized paginator object and updates it. Did I get it right?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.