当从 JavaScript 构造函数返回对象时(避免“new”),如何实现公共成员?
我已经开始编写我的“类”类型 JavaScript 函数,例如模块模式或揭示模块模式。这避免了使用“new”,并允许我在一个地方定义哪些函数是公共的。但是,我一直无法了解如何以这种模式使用和访问公共数据成员。
例如,下面的成员“id”可以通过 myobj.getId() 访问,但 myobj.id 未定义。
function setup(){
var myobj = MyObject();
myobj.initialize();
alert(myobj.getId()); // returns 3, as expected
alert(myobj.id); // returns undefined
}
function MyObject(){
var id;
function initialize(){
//do some stuff that initializes data including...
id = 3;
}
return{
initialize:initialize,
id:id,
getId:function(){ return id;}
}
}
有没有办法让 myobj.id 返回在initialize() 中设置的值?
I have begun writing my 'class' type JavaScript functions like the Module Pattern or Revealing Module patten. This avoids the use of 'new' and allows me to define which functions are public in a single place. However, I have been unable to see how to use and access public data members in this pattern.
e.g. the member 'id' below is accessible through myobj.getId(), but myobj.id is undefined.
function setup(){
var myobj = MyObject();
myobj.initialize();
alert(myobj.getId()); // returns 3, as expected
alert(myobj.id); // returns undefined
}
function MyObject(){
var id;
function initialize(){
//do some stuff that initializes data including...
id = 3;
}
return{
initialize:initialize,
id:id,
getId:function(){ return id;}
}
}
Is there no way to get myobj.id to work returning the value that was set in initialize()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的问题是你没有利用关闭的优势。
id:id
行创建一个新变量作为您要返回的对象文字的成员。它具有默认值未定义
。然后,您再次将相同的变量id
分配回其自身,undefined
。此外,模块是单个对象实例,而不是工厂函数。尝试这样的事情:
模块模式的核心是一个自执行的匿名函数,它声明变量,然后返回一个通过闭包对这些变量具有特权访问的对象。
末尾的额外括号(这里包含一个带有一些传入选项的对象文字)执行该函数,然后该函数返回一个对象并将其分配给
myModule
。任何声明为该返回对象成员的内容都可以公开访问。即使函数返回后,返回对象中的代码也可以通过闭包访问匿名函数中定义的变量。匿名函数中声明的变量实际上是私有的。函数外部的代码无法寻址它们,除非通过返回对象中提供的方法。
整个过程会在
myObject
中生成一个对象实例。最多只能创建一个,这是模块的定义。然而,可以采用类似的方法来创建工厂函数。Your problem is that you aren't taking advantage of closure. The line
id:id
creates a new variable as a member of the object literal you are returning. This has the default valueundefined
. Then you are assigning the same variableid
back to itself,undefined
again.Also, a module is a single object instance, not a factory function. Try something like this:
The core of the module pattern is a self executing anonymous function that declares variables and then returns an object that has privileged access to those variables through closure.
The extra parens at the end, here containing an object literal with some options that get passed in, executes the function, which then returns an object and assigns it to
myModule
.Anything declared as a member of that returned object can be accessed publicly. Code in the returned object has access to the variables defined in the anonymous function through closure even after the function has returned. The variables declared in the anonymous function are effectively private. Code outside the function cannot address them except through the methods provided in the returned object.
The whole thing results in a single object instance in
myObject
. No more than one can be created, which is the definition of a module. A similar approach could be taken the create a factory function however.原因是您的变量
id
默认设置为undefined
,并且它的类型也将是undefined
。现在,因为未定义类型是一种原始类型,所以返回对象中的赋值将是一个值赋值,而不是引用。因此,obj.id
在实例中将变为undefined
,并且与构造函数中的id
变量不同。现在
initialize
将更改构造函数中的id
,而getId
将返回相同的id
,但是obj.id
将引用undefined
对象属性。运行整个内容以查看其按预期工作。
The reason is that your variable
id
is set toundefined
by default and it's type will beundefined
as well. Now, because undefined type is a primitive one your assignment in the returned object will be a value assignment, not reference. Soobj.id
will becomeundefined
in the instance and not the same as theid
variable in the constructor function.Now
initialize
will change theid
in the constructor function, andgetId
will return the sameid
, butobj.id
will refer to theundefined
object property.Run the whole stuff to see it works as expected.
您可以通过返回缓存对象而不是匿名对象来解决此问题,并仍然保留 Revealing 模块模式的语法糖。
例如:
You can solve this and still maintain the syntactic sugar of the Revealing module pattern by returning a cached object instead of an anonymous one.
eg: