开发者

Confusing Javascript class declaration

I have some third-party Javascript that has statements like this:

FOO = function() {
   ...functions() ... 
   return { hash }
}();

It is working as designed but I'm confused by it. Can anybody define what this structure is doing? Is it just a weird way t开发者_开发百科o create a class?


This is a technique that uses closure. The idiom is well-known, but confusing when you first see it. FOO is defined as the object that the outermost function() returns. Notice the parenthesis at the end, which causes the function to evaluate and return { hash }.

The code is equivalent to

function bar() {
   ...functions() ... 
   return { hash }
};

FOO = bar();

So FOO is equal to { hash }. The advantage of this is that hash, whatever it is, has access to stuff defined inside the function(). Nobody else has access, so that stuff is essentially private.

Google 'Javascript closure' to learn more.


Js doesn't really have classes, per se, but "prototypes". This means that no two objects are ever of the same "type" in the normal type-safe sense, and you can dynamically add members to one instance while leaving the other unmolested. (which is what they have done).

Believe it or not, the syntax they have used is probably the most lucid, as it doesn't try to hide behind some C-style class syntax.

Doug Crockford's Javascript: The Good Parts is a quick read, and the best introduction to OOP in js that I've come across.


That's not actually a class, just an object. I'd recommend reading this: http://javascript.crockford.com/survey.html

Because JavaScript doesn't have block scope, your choice is (mostly) to have all variable reside in global or function scope. The author of your snippet wants to declare some local variables that he doesn't want to be in the global scope, so he declares an anonymous function and executes it immediately, returning the object he was trying to create. That way all the vars will be in the function's scope.


The parans at the end make this the Module Pattern, which is basically a way to have a single instance of an object(Singleton) while also using private variables and functions.

Since there's closures hash, if it's itself an object or function, will have access to all variables declared within that anonymous Singleton object.


You're missing an open parens, but it is basically a way of usually hiding information within an object i.e. a way of setting up private and privelaged methods.

For example

var foo = (function() {
   /* function declarations */
   return { /* expose only those functions you 
               want to expose in a returned object 
            */ 
          }
})();

Take a look at Papa Crockford's Private Members in JavaScript. This is basically the pattern you are seeing, but in a slightly different guise. The function declarations are wrapped in a self-invoking anonymous function - an anonymous function that is executed as soon as it's declared. Since the functions inside of it are scoped to the anonymous function, they will be unreachable after the anonymous function has executed unless they are exposed through a closure created by referencing them in the object returned from the execution of the anonymous function.

It's generally referred to as the Module Pattern.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜