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

KnockoutJS运行报错Uncaught TypeError: this.firstname is not a function求助

报错原因

核心错误为属性名大小写不匹配:

  • 定义可观察属性时使用的命名是firstName、lastName,其中Name的首字母为大写
  • 在ko.computed计算逻辑中调用时写为firstname、lastname,name的首字母为小写,和定义的属性名不一致,访问了不存在的属性,因此触发「不是函数」的类型错误。
修正后的代码
<span data-bind="text: fullname"></span>

<script type="text/javascript">
function AppViewModel() {
  this.firstName = ko.observable('Bob');
  this.lastName = ko.observable('Smith');

  this.fullname = ko.computed(function() {
    // 修正为和定义一致的大小写写法
    return this.firstName() + " " + this.lastName();
  },this);
}

var vm = new AppViewModel();
ko.applyBindings(vm);
</script>

内容的提问来源于stack exchange,提问作者原裕貴

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 07:57:03