继续 JavaScript“类” - 内的枚举
从之前的问题中,我有以下内容:
所以我实现了一个资源类,现在我想继续扩展它并添加所有常量和枚举(或者在 JS 允许的范围内......)。
这就是我目前所拥有的:
var resources = {
// images
player : new c_resource("res/player.png"),
enemies : new c_resource("res/enemies.png"),
tilemap : new c_resource("res/tilemap.png")
};
这就是我想继续将其扩展为:
var resources = {
// images
player : new c_resource("res/player.png"),
enemies : new c_resource("res/enemies.png"),
tilemap : new c_resource("res/tilemap.png"),
// enums
directions : {up:0, right:1, down:2, left:3},
speeds : {slow: 1, medium: 3, fast: 5}
};
...
function enemies() {
this.dir = resources.directions.down; // initialize to down
}
当我尝试访问 resources.directions.up 时,我的 JS 脚本会陷入一堆燃烧的代码中。在这种情况下是否允许使用枚举,如果不允许,我如何正确插入它们以在正常函数之外使用?我也尝试将它们定义为全局以达到类似的效果。
编辑:修复了逗号...这只是转录过程中的一个错误。
当我在 Firefox 中运行它并查看控制台时,我收到一条错误消息,指出资源未定义
。
资源“类”是在我的脚本顶部定义的,而 functionoes()
直接跟随...所以根据我的理解,它应该仍然在范围内...
From a previous question, I have the following:
So I have implemented a resource class, now I'd like to continue extending it and add all my constants and enums (or as far as JS will allow...).
This is what I currently have:
var resources = {
// images
player : new c_resource("res/player.png"),
enemies : new c_resource("res/enemies.png"),
tilemap : new c_resource("res/tilemap.png")
};
And this is what I would like to continue to extend it to:
var resources = {
// images
player : new c_resource("res/player.png"),
enemies : new c_resource("res/enemies.png"),
tilemap : new c_resource("res/tilemap.png"),
// enums
directions : {up:0, right:1, down:2, left:3},
speeds : {slow: 1, medium: 3, fast: 5}
};
...
function enemies() {
this.dir = resources.directions.down; // initialize to down
}
When I attempt to access resources.directions.up, my JS script goes down in a flaming pile of burning code. Are enums allowed in this context, and if not, how can I properly insert them to be used outside of a normal function? I have also tried defining them as global to a similar effect.
edits: fixed the comma...that was just an error in transcribing it.
When I run it in Firefox and watch the console, I get an error that says resources is undefined
.
The resources 'class' is defined at the top of my script, and function enemies()
directly follows...so from what I understand it should still be in scope...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
敌人
的调用堆栈是什么?你可以这样做:它可以工作一半,因为你可以在使用它们的代码“下方”声明函数,但稍后会失败,因为
f()
尝试使用foo
,目前尚未构建。What's the call stack to
enemies
? You could have done something like this:which works halfway since you can declare functions "below" the code that uses them, but fails later since
f()
attempts to usefoo
, which is not constructed at the moment.您在
tilemap : new c_resource(...)
之后缺少逗号,因此resources
从未正确分配,因此resources.directions.up
失败因为resources
未定义且未声明。You are missing a comma after
tilemap : new c_resource(...)
soresources
is never properly assigned, soresources.directions.up
fails becauseresources
is undefined and undeclared.它有效: http://jsfiddle.net/XfurM /2/
在 JavaScript 中,枚举可以通过简单地声明一个数组或对象来实现,使用文字符号:
或
It works: http://jsfiddle.net/XfurM/2/
In JavaScript, enums can be achieved by simply declaring an array or object, using the literal notation:
or