11

I am using module pattern in javascript. Is it a way to create instances of a "class" ? I am using it in a right way ?

var moduleClass = (function () {
var a =5;
return {
getA: function () {
  console.log(a);
 }
};
})();
var instance = moduleClass;
instance.getA();

http://jsfiddle.net/PzLKy/ How can I pass parameters on new instances ?

Petran
  • 7,677
  • 22
  • 65
  • 104
  • use a constructor function instead of a literal if you want to pass params at instantiation time. – dandavis Jul 18 '13 at 19:34
  • There's no class-like construct involved in your code. You just have a singleton generated with the module pattern. What do you *want* - only then we can tell you the code is correct? – Bergi Jul 18 '13 at 19:36
  • I want to be able to declare new instances and passing different values. I don't want to do something specific I am just learning. – Petran Jul 18 '13 at 20:00
  • What shell that `a` be? A static variable that all instances will share? An instance variable? Usually you don't need the module pattern to create a [class-like structure in JavaScript](http://stackoverflow.com/a/13418980/1048572). – Bergi Jul 18 '13 at 20:22

3 Answers3

13

You don't really need new here, Below is the right way to code to achieve what you are trying to achieve. Also, be really careful while using new, if used unwisely it can start clobbering your Global variable, If you want to use new, John Resig has a really nice explaination for how to do it the right way, for more read this article by John Resig http://ejohn.org/blog/simple-class-instantiation/

http://jsfiddle.net/PzLKy/2/

var moduleClass = (function () {
    var a =5;

  return {
      setA: function (inA) {
      a=inA;
    } ,
    getA: function () {
      alert(a);
    }

  };

})();


var instance = moduleClass;
instance.setA(8);
instance.getA();

Edit: contactmatt is right, definitely dont be afraid of using constructor, but here is some thing you need to be aware of

Taken from John Resig's article mentioned in the first paragraph,

suppose this is your code

function User(first, last){
    this.name = first + " " + last;
}

var user = new User("John", "Resig"); 
user.name // John Resig
var user2 = User ("first","last");
user2.name //undefined, also this would pollute your current scope

if you call the constructor, you would not get any kind of indication and can be a debugging nightmare.

a way to solve this is

function User(first, last){
  if ( this instanceof User ) {
    this.name = first + " " + last;
  } else
    return new User(first, last);
}

To Conclude,

So if you feel that constructor is the best way for your problem, use it. But be aware, also the simple class instantiation by John is a really useful pattern, try to go through it,he also explains generic constructor.

user2580076
  • 577
  • 2
  • 10
0

Use constructor functions. Don't be afraid of "new", just use it wisely.

Note: Standard naming convention is to name functions that will be used as function constructors with a capital letter. (i.e. ModuleClass instead of moduleClass)

function ModuleClass() {
  var a =5;
  return {
    getA: function () {
    console.log(a);
   }
  };
};

or if you're brave enough to learn about the 'this' keyword in JavaScript.

function ModuleClass() {
  var a =5;
  this.getA = function () {
    console.log(a);
  };
};

var instance = new moduleClass();
instance.getA();
contactmatt
  • 18,116
  • 40
  • 128
  • 186
-1

For creating instances you have to use the key word new with any function. This function aka Constructor can help you create multiple instances

var moduleClass = (function () {
var a =5;
return {
getA: function () {
  console.log(a);
 }
};
})();
var instance = new moduleClass;
instance.getA();

Constructor:Constructor functions are used with the new keyword, and they're one of the ways you give an object a prototype.

REFER-->

  1. Javascript Prototypes,objects,constructor??i am confused
  2. Constructors in JavaScript objects
Community
  • 1
  • 1
HIRA THAKUR
  • 17,189
  • 14
  • 56
  • 87
  • You said yourself that `new` must be used with a **function** … – Bergi Jul 18 '13 at 19:38
  • @MESSIAH: the code in the answer still throws a type error when i paste it into devtools... – dandavis Jul 18 '13 at 21:15
  • but i think the important thing is to understand how constructor is used,try directly writing the function,i.e without storing it in a variable.?contactmatt answered it the wright way i think – HIRA THAKUR Jul 18 '13 at 21:20