Javascript 对象字面量:值初始化?
我正在使用对象文字来创建带有方法的对象。
这是一个简单的例子。
var SizeManager = {
width : 800,
height : 600,
ratio : this.width / this.height,
resize : function (newWidth) {
width = newWidth;
height = newWidth / ratio;
}
}
我的问题是 SizeManager.ratio 返回“NaN”。我很确定这是一个初始化问题。
有没有办法获得正确的比率值?
有没有办法将构造函数或初始值设定项分配给对象文字?
定义构造函数对象是唯一的方法吗?
编辑:当然 SizeManager 理想情况下是一个单例(只有一个对象),这就是我使用对象文字的方式。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,这是一个初始化问题。
this
在您使用它时并不引用您的SizeManager
对象。 (对象初始值设定项不会更改this
的值。)this
由您调用函数的方式设置,并且在整个函数调用过程中具有相同的值。您没有调用那里的任何函数,因此this
具有该代码开始之前的任何值。(我在本文最后从您的具体示例中指出了一些有关比率的内容,但首先让我们针对您提出的一般情况介绍一些选项。)
Daniel 给了你关于使
ratio
成为函数的良好指导,但他没有似乎没有意识到你想要改变宽度。或者,如果width
和height
不会改变,只需稍后计算即可:(旁注:我已将
this.
添加到您在resize
中引用的属性在您的原始版本中缺失,但如果没有它们,您将处理 隐式全局变量的恐怖,这是一件坏事(tm)。)当然,您可以将所有这些封装到工厂中:
...但是您也可以将其作为实际的构造函数,这样您就不会创建大量重复(但相同)的
resize
函数:(请注意,我对最后一个名称进行了一些更改。)
最后一点是:在您的具体示例中,您实际上根本不需要存储
ratio
,您可以这样做:但这只是针对特定示例,因此上面的讨论讨论的是一般情况。
Yes, it's an initialization issue.
this
does not refer to yourSizeManager
object at the point you're using it. (Object initializers don't change the value ofthis
.)this
is set by how you call a function and has the same value throughout that function call. You're not calling any function there, sothis
has whatever value it had prior to the beginning of that code.(I've pointed out something about
ratio
from your specific example at the very end of this, but first let's walk through a few options for the general case you raise.)Daniel's given you a good steer on making
ratio
a function except he doesn't seem to have realized that you want to vary the width. Alternately, ifwidth
andheight
aren't going to change, just calculate it afterward:(Side note: I've added
this.
to the properties you're referencing inresize
. They were missing from your original, but they're required. Without them, you're dealing with the horror of implicit globals, which is a Bad Thing(tm).)Of course, you might encapsulate all of that into a factory:
...but then you might as well make it an actual constructor function so you don't create lots of duplicate (but identical)
resize
functions:(Note I changed the names a bit on this last one.)
And as one last final note: In your specific example, you don't actually need to store
ratio
at all, you could do this:But that's just for that specific example, hence the discussion above to talk about the general case.
如果您希望它根据
width
和height
的变化而变化,您的ratio
属性实际上应该是一个方法:另外,您可能想参考到
this.width
和this.height
而不是resize
中的width
和height
> 方法。Your
ratio
property should actually be a method if you want it to change based on changes towidth
andheight
:Also, you probably want to refer to
this.width
andthis.height
instead ofwidth
andheight
in theresize
method.在对象字面量中,
this
仅在函数内部时引用对象字面量本身。你可以尝试这样的东西而不是对象文字;this
将引用您在函数内部和外部创建的对象。这适用于单例模式。如果您需要多个 SizeManager,请将其设为构造函数而不是对象。
In an object literal,
this
only refers to the object literal itself when it is inside a function. You can try something like this instead of an object literal;this
will refer to the object you are creating both inside and outside of functions.That will work for a singleton pattern. If you need more than one SizeManager, make it a constructor function instead of an object.