Gandalf StormCrow
Gandalf StormCrow

Reputation: 26202

Question about JavaScript syntax

What kind of JavaScript is this Page.getProximityListCallback = function(obj) {; is this a function?

Surely it is but what is it with this syntax? Can someone explain?

Update: Thank you for your answers. Here is a follow up: Why then use function Page() {}? What is its purpose? Why not just call getProximityListCallback()?

Upvotes: -1

Views: 138

Answers (4)

Kimmo Puputti
Kimmo Puputti

Reputation: 256

You can have namespaces in your JavaScript code using objects like this:

var Page = {};
Page.getProximityListCallback = function (obj) {
    // ...
};

And remember that

function func(obj) {
    // ...
}

is the same thing as

var func = function (obj) {
    // ...
};

So in your example you are assigning an anonymous function to the Page object's getProximityListCallback member.

Upvotes: 0

user187291
user187291

Reputation: 53940

Functions are "first class" values in JavaScript, that is, you can have a constant of type "function" and assign it to a variable or object member, just like you do with numbers or strings. Compare

    var foo = "cow";

and

    var bar = function() { }

Conceptually there is no difference between these two lines.

Upvotes: 0

NilColor
NilColor

Reputation: 3542

This is something you can think of like adding a method to the Page class. In run-time. Method then will be accessible via the Page.getProximityListCallback() call.

Check here (JavaScript tab) - http://jsbin.com/arocu/edit.

BTW: it's a nice service to have fun with JavaScript.

Upvotes: 0

Mark Byers
Mark Byers

Reputation: 838156

It's an anonymous function which is then assigned to a variable.

http://helephant.com/2008/08/javascript-anonymous-functions/

Upvotes: 1

Related Questions