javascript 将参数传递给模块javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17732310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing parameter to a module javascript
提问by Petran
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 ?
我在 javascript 中使用模块模式。它是一种创建“类”实例的方法吗?我以正确的方式使用它吗?
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 ?
http://jsfiddle.net/PzLKy/如何在新实例上传递参数?
回答by user2580076
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/
你真的不需要 new 这里,下面是正确的编码方式来实现你想要实现的目标。另外,be really careful while using new, if used unwisely it can start clobbering your Global variable
如果您想使用 new,John Resig 对如何以正确的方式进行了非常好的解释,有关更多信息,请阅读 John Resig 的这篇文章
http://ejohn.org/blog/simple-class-instantiation/
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
编辑:contactmatt 是对的,绝对不要害怕使用构造函数,但这里有一些你需要注意的事情
Taken from John Resig's article mentioned in the first paragraph,
摘自第一段提到的 John Resig 的文章,
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.
因此,如果您觉得构造函数是解决问题的最佳方法,请使用它。但是请注意,John 的简单类实例化也是一个非常有用的模式,请尝试通过它,他还解释了泛型构造函数。
回答by contactmatt
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)
注意:标准命名约定是使用大写字母命名将用作函数构造函数的函数。(即 ModuleClass 而不是 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.
或者,如果您有足够的勇气学习 JavaScript 中的“this”关键字。
function ModuleClass() {
var a =5;
this.getA = function () {
console.log(a);
};
};
var instance = new moduleClass();
instance.getA();
回答by HIRA THAKUR
For creating instances you have to use the key word new
with any function.
This function aka Constructor can help you create multiple instances
要创建实例,您必须在new
任何函数中使用关键字。这个函数又名构造函数可以帮助你创建多个实例
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.
构造函数:构造函数与 new 关键字一起使用,它们是您为对象提供原型的方法之一。
REFER-->
参考-->