首页 > 解决方案 > 如何修复“未捕获的语法错误:无效的速记属性初始值设定项”

问题描述

即使 VSCODE 显示代码中没有错误,为什么我会遇到这个问题

var person={
        firstname:"koushik",
        sayHi : function()
        {
            console.log("hello there "+this.firstname)
        },
        check : function()
        {
            console.log (this===person)
        }
    }
    console.log(person)

我期望 person 对象的输出,但收到错误 Uncaught SyntaxError: Invalid shorthand property initializer

编辑:键firstname在分配值时有语法错误,=而不是:

标签: javascript

解决方案


var person={
    firstname:"koushik",
    sayHi : function()
    {
        console.log("hello there "+this.firstname)
    },
    check : function()
    {
        console.log (this===person)
    }
}
console.log(person)

您可以使用此代码。有什么问题在这里看到https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Missing_colon_after_property_id

另一个提示:尝试在 Firefox 的 Web 控制台中运行您的代码


推荐阅读