You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Knockout报错:Uncaught ReferenceError: Invalid left-hand side in assignment 排查

解决Knockout.js中"Invalid left-hand side in assignment"错误

你碰到的这个报错是Knockout.js开发里非常常见的新手问题——你误用了普通变量的赋值方式来更新Knockout的observable属性。

错误原因详解

在Knockout中,usersProfile.friendRequestStatus是一个observable对象,它本质是一个函数:

  • 要获取它的值时,你需要调用不带参数的函数:usersProfile.friendRequestStatus()
  • 要设置它的值时,你需要把新值作为参数传递给这个函数:usersProfile.friendRequestStatus('DECLINED')

你原来写的usersProfile.friendRequestStatus() = 'DECLINED';相当于试图给一个函数调用的返回值赋值,这在JavaScript语法里是完全不允许的,所以浏览器会抛出"Invalid left-hand side in assignment"这个语法错误。

修改后的代码

把你removeFriend方法里的错误行替换掉,修正后的完整方法代码如下:

usersProfile.removeFriend = function(parent) {
    $.get("http://localhost:8080/dashboard/friendrequest/remove/" + parent.user.userName(), function(data, status) {
        if (data.isSuccessful) {
            // 这里是修正后的赋值方式
            usersProfile.friendRequestStatus('DECLINED');
            showNotification('top-right', 'success', 'user has been removed', 250, 2500);
        }
    });
};

小提醒

以后操作Knockout的observable时记住这个规则:取值无参,赋值传参,就能避免这类语法错误啦。

内容的提问来源于stack exchange,提问作者appzone_oto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:29:03