Knockout.js:函数参数未定义 [英] Knockout.js: Function parameter undefined

查看:95
本文介绍了Knockout.js:函数参数未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的示例,无法正常工作.

I have a very simple example that is not working.

jsfiddle: http://jsfiddle.net/ThomasDeutsch/8hzhp/3/

jsfiddle: http://jsfiddle.net/ThomasDeutsch/8hzhp/3/

// My Model
function Customer(id, name, lastname) {
    this.Id = ko.observable(id);
    this.Name = ko.observable(name);
    this.LastName = ko.observable(lastname);
}

// My ViewModel
ViewModel = (function () {
    var getName = ko.computed(function (x) {
        return x.Name();
    });

    return {
        getName: getName(new Customer(1, "Thomas", "D"))
    };
})();

ko.applyBindings(ViewModel);​

问题:参数(x)未定义

目标:返回被调用对象的名称属性-我想将x用作属性,以便我可以对具有可观察名称属性的任何对象调用此函数

goal: return the Name-Property of the called Object - i want to use the x as a property so that i can call this function with any Object with a observable Name property

代码说明: 这是通过使用带有淘汰表(snockout.js)的揭示模块模式来完成的.名称属性是ko.observable()-因此需要().

Code explanation: This is done using the revealing-module-pattern with knockout.js. The Name-property is a ko.observable() - so the () is needed.

问题:为什么x不确定?

推荐答案

我问你.您认为x在哪里定义?

Let me ask you. Where do you think x is defined?

您正在调用getName并传递一个Customer,但是getName不需要参数.

You are calling getName and passing a Customer, but getName doesn't expect a parameter.

如果您这样重写函数,它将起作用:

If you rewrite your function like this it will work:

var getName = function(x) { return ko.computed( function() {
    return x.Name();
})};

这篇关于Knockout.js:函数参数未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆