admin管理员组文章数量:1425789
When using the module pattern in javascript how should constructors be defined, if at all. I would like my constructor to fit into a standard module pattern and not be global.
Why doesn't something like this work, is it plete and total nonsense?
var HOUSE = function() {
return {
Person: function() {
var self = this;
self.name = "john";
function name() {
return self.name;
}
}
};
}();
var me = new HOUSE.Person();
alert(me.name());
When using the module pattern in javascript how should constructors be defined, if at all. I would like my constructor to fit into a standard module pattern and not be global.
Why doesn't something like this work, is it plete and total nonsense?
var HOUSE = function() {
return {
Person: function() {
var self = this;
self.name = "john";
function name() {
return self.name;
}
}
};
}();
var me = new HOUSE.Person();
alert(me.name());
Share
Improve this question
edited Jan 19, 2012 at 16:21
rogermushroom
asked Jan 19, 2012 at 16:15
rogermushroomrogermushroom
5,5964 gold badges45 silver badges69 bronze badges
2
-
self.name
is a string"john"
so you can't invoke it. Note that your local functionname
is not exported anywhere. – Raynos Commented Jan 19, 2012 at 16:29 - Updated question, but it made a perfectly good answer make no sense so changed it back. – rogermushroom Commented Jan 19, 2012 at 16:34
3 Answers
Reset to default 3Your code is almost fine. However the function name()
was not public but the variable was so you were trying to execute the variable causing an error. Add the function getName
onto the object and call that instead:
var HOUSE = function() {
return {
Person: function() {
var self = this;
self.name = "john";
self.getName = function() {
return self.name;
}
}
};
}();
var me = new HOUSE.Person();
alert(me.getName());
http://jsfiddle/8nSbP/
Using var
and function foo() {}
(the latter as a declaration, which means "just" function foo() {}
without assigning it), create local symbols. So, the function is not available outside the constructor.
Whatever you want to expose (make public), you should assign to this
(or self
since you defined self = this
):
self.getName = function() {
return self.name;
};
Note that you already used name
, so I gave function another name. If you wanted to make the name
string local, and expose the function, then they can have the same name since there is no conflict. E.g.:
var name = "john";
self.name = function() {
return name;
};
You need to bring the method out, and attach it to the Person prototype. But when you do, you'll have a name property, and a name method, which won't work, so consider renaming the latter
HOUSE.Person.prototype.getName = function(){
return this.name;
}
OR, you could just attach it to this
, and make getName a privileged method:
Person: function() {
this.name = "john";
this.getName = function() {
return this.name;
}
}
本文标签: javascriptConstructors in the Module PatternStack Overflow
版权声明:本文标题:javascript - Constructors in the Module Pattern - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745398118a2656910.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论