(function (G, U){ "use strict"; var $ = G.jQuery, bool = "boolean", string = "string", number = "number", object = "object"; }(this, undefined)); G and U are passed to the anonymous function, which are assigned the values of this and undefined respectively. Question: what in this case will be equal to this ? It depends on which platform you are working with, but in any case it will be a global namespace, such as in browsers, for example, the window object. "use strict"; for (i = 0; i < items.length; i += 1){ //- } i has already been declared, and this cycle is nested: for (i = 0; i < myArr.length; i += 1){ // 50 , , for (i = 0; i < myArr.length; i += 1){ //- } } alert(" : "+ error); error variable has not been previously declared, in normal mode, the user will be able to admire the message " : undefined" . In strict mode, this variable must be at least defined."use strict"; ."use strict"; , including a strict mode, there is a description of several variables. As you can see, I follow the “one var statement” pattern, which is also recommended for use. Agree, the construction described above does not look as bad as the one below: var $ = G.jQuery; var bool = "boolean"; var string = "string"; var number = "number"; var object = "object"; bool , string , number and object further described below for more convenience: if (params.hasOwnProperty("title") && typeof params.title === string){ //- result.title = params.title; } if (p.hasOwnProperty("title") && typeof p.title === s){ r.title = p.title; } (function (G, U){ "use strict"; var id = 0, PREFIX = "my-library-id-prefix-"; function getNewId(){ id += 1; return PREFIX + id.toString(); } G.createId = getNewId; // getNewId() createId }(this, undefined)); function Div(params){ //- <div> var id = getId(); //id , this.show = function(){ $("<div />", { "id": id }); } } var newDiv = new JSui.Div({ width: 200, height: 150 }); (function(G, U){ "use strict"; var UI= G.JSui || {}; // function Div(){ ... } UI.Div = Div; G.JSui = UI; }(this, undefined)); (function(G, U){ "use strict"; var UI= G.JSui || {}; // function Label(){ ... } UI.Label = Label; G.JSui = UI; }(this, undefined)); G.JSui = UI; clearly too much. But do not make hasty conclusions! What if the specified module is connected first and the global JSui variable JSui not yet defined? The local UI variable will be assigned an empty object {} . Further, in the module code, we expand it, but export to the global namespace does not occur until the line is called: G.JSui = UI; JSui object JSui already defined, the local UI variable receives a reference to it and expands it with new properties and methods. In this case, indeed, the above assignment will be redundant, however, one should not forget the simple fact that in this case the object will be passed by reference and performance will not be affected.Source: https://habr.com/ru/post/231883/
All Articles