I am reading and trying some examples of self executing JavaScript function. I found a few examples on how to call them, but I am still a little confused as to the proper way to create and call them. For example, If I have a foo method inside this self executing function that takes 2 parameters, param1 and param2. How do I call this method? You can pass global objects such as window, document, etc. What about parameters that are not global but need in order for the function to perform some action?
Here is an example:
foo.js
(function (window, document, $, undefined) {
function foo(param1, param2) {
//do stuffs here
}
})(window, document, jQuery);
index.html:
<script src="~/Scripts/Custom/foo.js"></script>
<script type="text/javascript">
var myFoo = new Foo("parameter1","parameter2");
</script>
Foo. Thefooyou did define is not referenceable through the global namespace so you can't use it externally (you would need to set up some reference first). undefined is non-configurable, non-writable in the global namespace, creating it as a property like this makes it writable so can be considered bad practice.