开发者

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 fo开发者_开发技巧r your answers. Here is a follow up: Why then use function Page() {}? What is its purpose? Why not just call getProximityListCallback()?


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

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


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.


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.


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.


possibly Page can have other related methods

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜