3

I some times create class like this

function class1(){
.....
   class1.prototype.callme = function(){
      alert("hai");
   }
}
then I instantiate using (new class1()).callme();

and some times I use modular pattern

var class2 = (function(){
var privatemethod = function(){
....
}
var publicmethod = function(){
alert("am public");
}
return{
callme:publicmethod
}
})();

then I call class2.callme()

Whats the advantage and disadvantage, can some body please explain.

3
  • Why are you creating properties/methods on the prototype inside the constructor function? In your first example class1.prototype.callme is overwritten every time you create a new class1 instance, which is kind of inefficient. And if you actually use (new class1()).callme() (i.e., that's not a simplification for this question) the whole class thing is kind of redundant. Commented Aug 3, 2012 at 4:20
  • its just an example, but I just want the advantage and disadvantage Commented Aug 3, 2012 at 4:25
  • 1
    The first pattern as shown is never a good plan - it's kind of an anti-pattern. Instantiating an object just to call a method from its prototype doesn't help you much and the second pattern will always be better. If in reality you actually do something with individual instances of class1 then that serves a different purpose to the second example but it's kind of hard to compare the two when you don't give a more realistic example. Commented Aug 3, 2012 at 4:30

3 Answers 3

2

The first one is a constructor the second one is an object, so they're not the same. You might want to use one or other in different situations. In any case you usually declare the prototype outside the constructor and use this.property for public methods and variables. The second option is not reusable as a class, unless you use something like jQuery's extend or Underscore's _extend, which is the way I usually do it, it seems simpler without the prototype chain bloated code.

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

2 Comments

true I agree, also some time when you use fire bug, the first method doesn't reveal the code but the second one does, will that also count as well ? correct me if am wrong
can you give me an example for extend ? did you mean create same methods on new class(call it as class3) and when you extend, the class2's method will override the class3's method ? sorry if am confusing you :)
2

If you want to learn about the different Javascript patterns I would definitely recommend Learning JavaScript Design Patterns Its free/opensource and kept constantly updated.

Comments

1

For using Module pattern:

The freedom to have private functions which can only be consumed by our module. As they aren't exposed to the rest of the page (only our exported API is), they're considered truly private.

Given that functions are declared normally and are named, it can be easier to show call stacks in a debugger when we're attempting to discover what function(s) threw an exception.

As T.J Crowder has pointed out in the past, it also enables us to return different functions depending on the environment. In the past, I've seen developers use this to perform UA testing in order to provide a code-path in their module specific to IE, but we can easily opt for feature detection these days to achieve a similar goal.

1 Comment

Although that's a good blog post in a general sense, it doesn't really relate to this question at all.

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.