I have been studying framework development for several weeks, and I came across something that is highly recommended and puts pressure on the lib development world by immediately calling anonymous functions.
I can never make it work, and I have not been able to find a resource that explains usage and logic in detail.
Here is what I know so far:
- He immediately calls - he starts everything anonymously, immediately.
- This is anonymous - it has no name, so the code inside it is not accessible by external code.
- You can pass global window, object, and undefined parameters . That’s all I know about it, but don’t understand them completely.
I am looking not only for a detailed resource, but also one that explains the logic of this. Because I find it very illogical.
Here is what I have:
(function( window, document, undefined ) {
window.myThingy = myThingy;
var myThingy = function() {
};
myThingy.prototype = {
constructor: myThingy,
create: function( elementToBeCreated ) {
return document.createElement( elementToBeCreated );
}
};
})( window, document );
Then
myThingy().create("div");
But he still says he is myThingy() [object]not a function.
What am I doing wrong? Why should I use the calling functions right away and not just create a global object myThingy = function()? Why should I use window?
I know that there are several resources on the network, but I can’t understand anything. Some of them go halfway into the details, some of them try to go into the details, but cannot explain critical things. Why is this so emphasized in the design of the structure?
Don't worry, I'm not trying to “reinvent the wheel”, but I'm trying, however, to learn JavaScript , not just pre-packaged things.
: