0

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.

1
  • It does not make a difference. Benefits are still the same. Though if that means that you only create one instance of that constructor function, then you simply use an object literal. Commented Jan 16, 2013 at 9:34

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I see, so the first example is more similar to PHP (in that creation of object each time will replicate methods). I am guessing the same thing applies if the prototype has a constructor?
Hmmm... no, it's not this way. Using "prototype" means that every instance of the object will share the same method, that is accessed by reference. So this is more PHP like. Without "prototype" each instance will have its own copy of the method. -- But comparing Javascript to PHP (or Classy languages with C-like syntax lake C++ or Java) could lead you to wrong conclusions or bad programming patterns as JavaScript is a classless and functional language. I'd suggest to learn a bit of JS fundamentals.
...I write that because I've made the same mistake in the past! I thought it looks like C, let's treat it like is C with some little differences. No, bad idea. It's a whole different thing!
1

In this case I think they're pretty much equivalent. Both are going to be interpreted each time the outer anonymous function executes.

So as long as you call only one new MyObject() inside, it doesn't matter. If you call it more than once though, you're better off with the prototype version.

Comments

1

I don't think it makes a difference. The IIFE (Immediatly Invoked Function Expression) is to protect variables from leaking to global scope.

Both MyObject are constructors but the first one has a function as unique property of that instance (will be cloned when invoking new). On the second example you add a method to the prototype of that constructor function, making it available to all instances, that's why the second example is preferred AFAIK.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.