In one JavaScript file, the script was wrapped into this, why wrap codes into this? Any reference to this? Why define Basepage=global.Basepage
?
;(function (global,$,_) {
var Basepage=global.Basepage = Backbone.View.extend({});
}(app,jQuery,_));
In one JavaScript file, the script was wrapped into this, why wrap codes into this? Any reference to this? Why define Basepage=global.Basepage
?
;(function (global,$,_) {
var Basepage=global.Basepage = Backbone.View.extend({});
}(app,jQuery,_));
By setting a var Basepage
, Basepage will become a local variable in that function, and they don't need to write global.Basepage
for any further references. Organization like this has several benefits, and I'll try to list each.
var x
or var element
and don't want it to get mixed up with others as a global variable.app
will have BasePage
as a property.It is a method of namespacing
. With this you get the effect on adding a single variable into the global namespace and you can access it to access other methods and variables.
What it is doing is self executing the function when the code is read, and when it executes, app, jQuery, and _
are passed in as variables to the function, which are what the parameters take on. global = app
$ = jQuery
_ = _
.
This gives you a good way to encapsulate functionality without polluting the global namespace and decreases the risk over overriding variables that have the same name in your application.