继续 JavaScript“类” - 内的枚举

发布于 2024-10-11 09:08:00 字数 1136 浏览 8 评论 0原文

从之前的问题中,我有以下内容:

所以我实现了一个资源类,现在我想继续扩展它并添加所有常量和枚举(或者在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

錯遇了你 2024-10-18 09:08:00

敌人的调用堆栈是什么?你可以这样做:

var foo = {
  bar: 1,
  f_rv: f()
};

function f() {
  return foo.bar;
}

它可以工作一半,因为你可以在使用它们的代码“下方”声明函数,但稍后会失败,因为 f() 尝试使用 foo ,目前尚未构建。

What's the call stack to enemies? You could have done something like this:

var foo = {
  bar: 1,
  f_rv: f()
};

function f() {
  return foo.bar;
}

which works halfway since you can declare functions "below" the code that uses them, but fails later since f() attempts to use foo, which is not constructed at the moment.

野味少女 2024-10-18 09:08:00

您在 tilemap : new c_resource(...) 之后缺少逗号,因此 resources 从未正确分配,因此 resources.directions.up 失败因为 resources 未定义且未声明。

You are missing a comma after tilemap : new c_resource(...) so resources is never properly assigned, so resources.directions.up fails because resources is undefined and undeclared.

春夜浅 2024-10-18 09:08:00

它有效: http://jsfiddle.net/XfurM /2/

在 JavaScript 中,枚举可以通过简单地声明一个数组或对象来实现,使用文字符号:

var weekdays = [
    'Monday',
    'Tuesday', 
    'Wednesday', 
    'Thursday', 
    'Friday', 
    'Saturday', 
    'Sunday'
];  

var weekdays = {
    Monday: 1,
    Tuesday: 2,
    Wednesday: 3,
    Thursday: 4,
    Friday: 5,
    Saturday: 6,
    Sunday: 7
};

It works: http://jsfiddle.net/XfurM/2/

In JavaScript, enums can be achieved by simply declaring an array or object, using the literal notation:

var weekdays = [
    'Monday',
    'Tuesday', 
    'Wednesday', 
    'Thursday', 
    'Friday', 
    'Saturday', 
    'Sunday'
];  

or

var weekdays = {
    Monday: 1,
    Tuesday: 2,
    Wednesday: 3,
    Thursday: 4,
    Friday: 5,
    Saturday: 6,
    Sunday: 7
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文