Just out of curiosity, is there a special reason why you have a separate init method?
The function that defines your “class” is called the “constructor”, and you can just do the customization there. If you want to “reinitialize” an object, then this may be useful, but it doesn't seem to serve here.
For instance:
"Wrapper"
(function () {
This is basically pointless, but in the end you will have to start doing, so now you can start. This is one way to "wrap", there are others.
Basically, your script file was written, if the user ran another script that had the MyClass function, he could overwrite yours or vice versa, causing problems.
"wrapping" stores all this in this function. If you need to do something accessible to external things, you can expose it.
for the comment:
You can access functions and variables from within the wrapper by exposing them to the outside:
var myApp = (function myApp(){
Please note that in this example we just expose the constructor, if you need an instance of an object you will have to collect them in a variable and expose this variable as:
var theInstance = new myClass(); return { theInstance : theInstance };
Now it will be available outside myApp as myApp.theInstance
You can also use a more general packaging scheme:
var myApp = { myClass: function(){
Here myApp is just an object literal containing your functions, etc. The main difference is that EVERYTHING in myApp can be accessed externally via myApp.name or myApp [name];