1
;(function(Register, $, undefined) {

'use strict';

Register.Model = {
    Uid: ''
};

Register.Handler= {
    init: function() {
        Register.Model.Uid= $('body').data('uid');
    }
};

})(window.Register= window.Register|| {}, jQuery);

Hello, I'm new to javascript and JQuery, the last line of the code above really confuses me. Please help me understand that syntax. Any books that I can learn from please?

Joe Dyndale
  • 1,073
  • 1
  • 15
  • 32
Uda
  • 87
  • 2
  • 9

2 Answers2

5

first look at this:

(function(){
   console.log("executed");
})();

the code defines a function then immediately execute it, you can also pass parameter into the function, for example:

(function(a,b){
    console.log(a+b);//should print 3
})(1,2);
ltebean
  • 1,019
  • 6
  • 15
4

There are many things worth mentioning in that code.

1) This is a definition of a function which is called immediatly afterwards

(function(r, $, undefined){
   // some stuff
})(window.Register= window.Register|| {}, jQuery);

2) Note that

window.Register= window.Register|| {}

is passed as a first argument. This means that window.Register is set to window.Register if it already exists (actually if it evaluates to boolean true, which is close enough) or to new object {} otherwise. Since = operator returns thing on the right side this entire syntax is a shortcut for:

if (!window.Register) {
    window.Register = {};
}
(function(r, $, undefined){
   // some stuff
})(window.Register, jQuery);

3) The function accepts 3 arguments, however 2 were passed. Therefore the last argument named undefined will be undefined (which is supposed to be a keyword but someone may override it).

4) As for this line

'use striction';

it's probably a mistake and it should be 'use strict';. It tells interpreter to use more strict rules (helpful for avoiding for example accidental globals). Read this for more info:

What does "use strict" do in JavaScript, and what is the reasoning behind it?

5) The semicolon ; in front is used because JavaScript allows to write code without semicolon in most but not all cases. In order to avoid potential errors putting a semicolon in front is necessary (assuming there actually is something in front of that code).

Hope it helps.

Community
  • 1
  • 1
freakish
  • 54,167
  • 9
  • 132
  • 169
  • 1
    Some extra reading to complement this answer, [Immediately Invoked Function Expressions](http://benalman.com/news/2010/11/immediately-invoked-function-expression/), and [Essential JS Namespacing](http://addyosmani.com/blog/essential-js-namespacing/) – kieran May 08 '13 at 06:39
  • 1
    And the semicolon first makes sure that you end someone elses script before running yours. – Henrik Andersson May 08 '13 at 06:44
  • Yeah, having a parameter named `undefined` seems like a terrible think to me. If you passed in more than two parameters to this function, any tests of `something === undefined` anywhere inside that function or functions called inside that function etc. would produce entirely unpredictable results! – Joe Dyndale May 08 '13 at 06:45
  • 2
    @JoeDyndale that would be true, _if you did_, but the function is immediately invoked with just two arguments and as no reference to the function is left around it's impossible for someone else to call it later with more. – Alnitak May 08 '13 at 06:48
  • @Alnitak Good point. So it's basically entirely redundant then. – Joe Dyndale May 08 '13 at 06:50
  • 1
    @JoeDyndale not at all, it's actually a guard against _someone else_ setting `undefined` to an odd value outside of this function's scope before it's invoked. – Alnitak May 08 '13 at 06:52
  • @Alnitak Ah, of course. Thanks for clarifying that. Safe-guards all around I see :) Cool. – Joe Dyndale May 08 '13 at 06:53
  • @JoeDyndale it's entirely useless, since (a) it's never used in the body of that function and (b) you could just use `void 0` instead (or `false.x` for that matter, or any number of things). – Dagg Nabbit May 08 '13 at 06:53
  • @DaggNabbit a) It's a snippet b) It's a different way of achieving the same thing. It's definitely not useless. – freakish May 08 '13 at 06:56
  • @JoeDyndale, it may be intended as a safeguard against that, but it's *entirely useless* in this case since it is *never used*. – Dagg Nabbit May 08 '13 at 06:57
  • @JoeDyndale Yeah, I've edited my comment. In my opinion: this code is a snippet as it doesn't make much sense anyway (there's no need to wrap everything in a function). Besides it might be someone's coding standard. – freakish May 08 '13 at 06:58
  • @DaggNabbit Yeah, but I agree with freakish :) – Joe Dyndale May 08 '13 at 06:59
  • "there's no need to wrap everything in a function" ... I think that's an important point to make here. If we aren't doing that, then we can just say `var undefined` in whatever scope we intend to use it, and we can leave it out of snippets that *don't* use it. Either way, it doesn't belong in this example. – Dagg Nabbit May 08 '13 at 07:04
  • Thank you all, specially @freakish, it helps a lot! – Uda May 08 '13 at 07:04
  • BTW, `undefined` is not "supposed to be a keyword," it's a *property of the global object*, which is why you can reassign ("override") it. If it were a keyword, trying to assign a value to `undefined` would produce a syntax error. – Dagg Nabbit May 08 '13 at 07:22
  • @DaggNabbit if that's correct (I didn't check the spec yet) then another (small) advantage of defining it locally is that it avoids having to traverse the scope chain when looking up the variable. – Alnitak May 08 '13 at 08:16
  • @Alnitak, yeah, I think that's the reasoning behind it; same with passing in `jQuery` (at the expense of creating hideous abominations like this, of course). I'd be curious how it compares to `void 0`, though. – Dagg Nabbit May 08 '13 at 08:27