;(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?
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);
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.