mirageglobe
mirageglobe

Reputation: 3134

Creating functions in objects using javascript

I came across the following recently, which seems to be an object declaration, starting with just a semi-colon. It works fine.

;(function() { var ..... = this; })()

Are there any difference in declaring it this way or if there are alternatives?

Upvotes: 0

Views: 38

Answers (2)

Amit Joki
Amit Joki

Reputation: 59292

There are a lot of concepts you've got wrong.

  1. Though functions are objects too, this isn't a object declaration, it's a normal function declaration.
  2. The functional form you've used is self-invoking function.
  3. The semi-colon ; is used so that the code doesn't break when several scripts are minified into a single file.

Upvotes: 2

brroshan
brroshan

Reputation: 1650

"functions in objects" are called methods.

var foo = {
     x: function() {} // method
} 

Upvotes: 0

Related Questions