I lack some fundamental understanding in function and variable declaration in javascript and am struggling to really understand it.
I have this function "urlParam" which I place in a general file ('reusable.js') to be reused:
(function (ko, $) {
var urlParam = function (name, w) {
w = w || window;
var rx = new RegExp('[\&|\?]' + name + '=([^\&\#]+)'),
val = w.location.search.match(rx);
return !val ? '' : val[1];
}
})(ko,jQuery);
Then I have a viewModel where I want to access this function, placed in a file 'viewmodel.js':
ViewModel = function(){
var userId = urlParam("UserId");
};
In my view.html I include both files and then, after the html, I bind the view model to the view with:
ko.applyBindings(new ViewModel());
This cause an reference error: ReferenceError: urlParam is not defined
However if I place the urlParam-function outside the (function (ko, $) {})(ko, jQuery); declaration it works perfectly.
What does this (function () {})();
do really?