I have 2 different ways using my Javascript objects inside an anonymous scope.
(function(){
function MyObject() {
this.MyMethod = function() {
//code here
}
}
first = new MyObject();
first.MyMethod();
})();
And
(function(){
function MyObject(){};
MyObject.prototype.MyMethod = function() {
//code here
}
first = new MyObject();
first.MyMethod();
})();
I am aware that the prototype version is better but am not sure if using an anonymous scope makes a difference to the benefits/drawbacks.
Using in anonymous scope makes no difference.
The benefits are the same as using in the global scope.
If you are instantiating MyObject a very high number of times “prototype” version will avoid replicating the method in each instance and so will avoid wasting of resources (memory).
If you are instantiating MyObject one or two times it will make no difference in an immediate function as in the global scope.