SyntaxError: missing name after . operator - JavaScript 编辑

The JavaScript exception "missing name after . operator" occurs when there is a problem with how the dot operator (.) is used for property access.

Message

SyntaxError: missing name after . operator

Error type

SyntaxError

What went wrong?

The dot operator (.) is used for property access. You will have to specify the name of the property that you want to access. For computed property access, you might need to change your property access from using a dot to using square brackets. These will allow you to compute an expression. Maybe you intended to do concatenation instead? A plus operator (+) is needed in that case. Please see the examples below.

Examples

Property access

Property accessors in JavaScript use either the dot (.) or square brackets ([]), but not both. Square brackets allow computed property access.

var obj = { foo: { bar: "baz", bar2: "baz2" } };
var i = 2;

obj.[foo].[bar]
// SyntaxError: missing name after . operator

obj.foo."bar"+i;
// SyntaxError: missing name after . operator

To fix this code, you need to access the object like this:

obj.foo.bar; // "baz"
// or alternatively
obj["foo"]["bar"]; // "baz"

// computed properties require square brackets
obj.foo["bar" + i]; // "baz2"

Property access vs. concatenation

If you are coming from another programming language (like PHP), it is also easy to mix up the dot operator (.) and the concatenation operator (+).

console.log("Hello" . "world");

// SyntaxError: missing name after . operator

Instead you need to use a plus sign for concatenation:

console.log("Hello" + "World");

See also

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:33 次

字数:3319

最后编辑:7年前

编辑次数:0 次

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