Javascript 数组/对象语法失败

发布于 2024-11-08 07:42:23 字数 460 浏览 0 评论 0原文

为什么我会在以下代码中收到此错误:Uncaught SyntaxError: Unexpected token { on line 1。

var cube_points = {
    {'x' : 100, 'y' : 100, 'z' : 100},
    {'x' : 100, 'y' : 100, 'z' : -100},
    {'x' : -100, 'y' : 100, 'z' : -100},
    {'x' : -100, 'y' : 100, 'z' : 100},
    {'x' : 100, 'y' : -100, 'z' : 100},
    {'x' : 100, 'y' : -100, 'z' : -100},
    {'x' : -100, 'y' : -100, 'z' : -100},
    {'x' : -100, 'y' : -100, 'z' : 100}
};

Why do I get this error with this code: Uncaught SyntaxError: Unexpected token { on line 1.

var cube_points = {
    {'x' : 100, 'y' : 100, 'z' : 100},
    {'x' : 100, 'y' : 100, 'z' : -100},
    {'x' : -100, 'y' : 100, 'z' : -100},
    {'x' : -100, 'y' : 100, 'z' : 100},
    {'x' : 100, 'y' : -100, 'z' : 100},
    {'x' : 100, 'y' : -100, 'z' : -100},
    {'x' : -100, 'y' : -100, 'z' : -100},
    {'x' : -100, 'y' : -100, 'z' : 100}
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

感受沵的脚步 2024-11-15 07:42:23

外部对象的元素有值但没有键。如果您想要一个立方体点数组,请使用方括号来表示数组文字:

var cube_points = [
    {'x' : 100, 'y' : 100, 'z' : 100},
    {'x' : 100, 'y' : 100, 'z' : -100},
    {'x' : -100, 'y' : 100, 'z' : -100},
    {'x' : -100, 'y' : 100, 'z' : 100},
    {'x' : 100, 'y' : -100, 'z' : 100},
    {'x' : 100, 'y' : -100, 'z' : -100},
    {'x' : -100, 'y' : -100, 'z' : -100},
    {'x' : -100, 'y' : -100, 'z' : 100}
];

如果您想要一个对象,请为项目提供一个键。这可以是数字、字母甚至对象:

var cube_points = {
    1: {'x' : 100, 'y' : 100, 'z' : 100},
    2: {'x' : 100, 'y' : 100, 'z' : -100},
    'q': {'x' : -100, 'y' : 100, 'z' : -100},
    'z': {'x' : -100, 'y' : 100, 'z' : 100},
    '25': {'x' : 100, 'y' : -100, 'z' : 100},
    '26': {'x' : 100, 'y' : -100, 'z' : -100},
    10: {'x' : -100, 'y' : -100, 'z' : -100},
    11: {'x' : -100, 'y' : -100, 'z' : 100}
};

显然,使用对象需要某种有序的键选择系统。由于您的点不容易命名,因此最好使用数组。但我想展示对象文字表示法是什么样子的。

Your outer object's elements have values but no keys. If you want an array of cube points, use the square bracket to denote an array literal:

var cube_points = [
    {'x' : 100, 'y' : 100, 'z' : 100},
    {'x' : 100, 'y' : 100, 'z' : -100},
    {'x' : -100, 'y' : 100, 'z' : -100},
    {'x' : -100, 'y' : 100, 'z' : 100},
    {'x' : 100, 'y' : -100, 'z' : 100},
    {'x' : 100, 'y' : -100, 'z' : -100},
    {'x' : -100, 'y' : -100, 'z' : -100},
    {'x' : -100, 'y' : -100, 'z' : 100}
];

If you want an object, give the items a key. This could be numbers or letters or even objects:

var cube_points = {
    1: {'x' : 100, 'y' : 100, 'z' : 100},
    2: {'x' : 100, 'y' : 100, 'z' : -100},
    'q': {'x' : -100, 'y' : 100, 'z' : -100},
    'z': {'x' : -100, 'y' : 100, 'z' : 100},
    '25': {'x' : 100, 'y' : -100, 'z' : 100},
    '26': {'x' : 100, 'y' : -100, 'z' : -100},
    10: {'x' : -100, 'y' : -100, 'z' : -100},
    11: {'x' : -100, 'y' : -100, 'z' : 100}
};

Obviously, using an object necessitates some kind of orderly system of key selection. Since your points aren't easily named, you're better off using an array. But I wanted to show what object literal notation would look like.

你如我软肋 2024-11-15 07:42:23

您所拥有的不是有效的 javascript 对象定义。 {} 表示对象,因此内部应该有属性,而不是其他 {}。您需要的是一个用 []: 表示的数组,

var cube_points = [
    {'x' : 100, 'y' : 100, 'z' : 100},
    {'x' : 100, 'y' : 100, 'z' : -100},
    {'x' : -100, 'y' : 100, 'z' : -100},
    {'x' : -100, 'y' : 100, 'z' : 100},
    {'x' : 100, 'y' : -100, 'z' : 100},
    {'x' : 100, 'y' : -100, 'z' : -100},
    {'x' : -100, 'y' : -100, 'z' : -100},
    {'x' : -100, 'y' : -100, 'z' : 100}
];

然后您可以像这样访问它:

cube_points[2].y

What you have is not a valid javascript object definition. {} denotes object, so inside you should have properties, not other {}. What you need is an array which is denoted with []:

var cube_points = [
    {'x' : 100, 'y' : 100, 'z' : 100},
    {'x' : 100, 'y' : 100, 'z' : -100},
    {'x' : -100, 'y' : 100, 'z' : -100},
    {'x' : -100, 'y' : 100, 'z' : 100},
    {'x' : 100, 'y' : -100, 'z' : 100},
    {'x' : 100, 'y' : -100, 'z' : -100},
    {'x' : -100, 'y' : -100, 'z' : -100},
    {'x' : -100, 'y' : -100, 'z' : 100}
];

which you could then access like this:

cube_points[2].y
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文