最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Passing parameter to a module javascript - Stack Overflow

programmeradmin0浏览0评论

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();

/ How can I pass parameters on new instances ?

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 ?

Share Improve this question asked Jul 18, 2013 at 19:31 PetranPetran 8,04723 gold badges71 silver badges111 bronze badges 4
  • use a constructor function instead of a literal if you want to pass params at instantiation time. – dandavis Commented Jul 18, 2013 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 Commented Jul 18, 2013 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 Commented Jul 18, 2013 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. – Bergi Commented Jul 18, 2013 at 20:22
Add a comment  | 

3 Answers 3

Reset to default 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.

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();

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
发布评论

评论列表(0)

  1. 暂无评论