Javascript 敲除计算属性输出函数体

Javascript 敲除计算属性输出函数体,javascript,mvvm,knockout.js,Javascript,Mvvm,Knockout.js,我是敲除新手,我试图得到一个计算属性的值,但得到的只是函数体。 我的模型是 var AuthorViewModel = function (data) { this.id = ko.observable(data.id); this.firstName = ko.observable(data.firstName); this.lastName = ko.observable(data.lastName); this.email =

我是敲除新手,我试图得到一个计算属性的值,但得到的只是函数体。 我的模型是

    var AuthorViewModel = function (data) {
    this.id        = ko.observable(data.id);
    this.firstName = ko.observable(data.firstName);
    this.lastName  = ko.observable(data.lastName);
    this.email     = ko.observable(data.email);
    this.phone     = ko.observable(data.phone)
    this.address   = ko.observable(data.address);
    this.fullName = ko.computed(function () {
        return this.firstName() + " " + this.lastName();
    },this);
};
但是我得到的不是混凝土线

function observable() { if (arguments.length > 0) { // Write // Ignore writes if the value hasn't changed if ((!observable['equalityComparer']) || !observable['equalityComparer'](_latestValue, arguments[0])) { observable.valueWillMutate(); _latestValue = arguments[0]; if (DEBUG) observable._latestValue = _latestValue; observable.valueHasMutated(); } return this; // Permits chained assignments } else {.....

AppViewModel

/


实际上,我使用的是来自的教程,只是做了一些修改

您的问题在于
newAuthor
创建部分:

var newAuthor = {
    id        : this.id,
    firstName : this.firstName,
    lastName  : this.lastName,
    email     : this.email,
    phone     : this.phone,
    address   : this.address,
 };
因为使用这段代码,您正在创建一个对象,它引用的是可观察对象本身,而不是它们的值

因此,在您的
AuthorViewModel
中,
数据
对象将具有可观察属性,您可以将这些属性重新包装为一组新的可观察属性

因此,在创建
newAuthor
时,需要展开观察对象:

var newAuthor = {
    id        : this.id(),
    firstName : this.firstName(),
    lastName  : this.lastName(),
    email     : this.email(),
    phone     : this.phone(),
    address   : this.address(),
};

如何尝试获取值?但是如果在返回之前使用console log,我会得到相同的输出。您还可以发布如何填写
作者
数组吗?你的
数据。firstName
数据。lastName
是否已经是可观察的?因为只有在这种情况下,您的代码才能生成您的输出……我想您有一点,我想我早就注意到了同样的想法,但不幸的是,我对javascript也是新手,但我不明白的是,为什么其他属性可以工作
 The root view model for the application
    var AppViewModel = function() {
        this.authors = ko.observableArray();
    };

    AppViewModel.prototype.addAuthor = function() {
        showModal({
            viewModel: new AddAuthorViewModel(),
            context: this // Set context so we don't need to bind the callback function
        }).then(this._addAuthorToAuthors);
    };

    AppViewModel.prototype._addAuthorToAuthors = function (newAuthorData) {
        this.authors.push(new AuthorViewModel(newAuthorData));
    };
var newAuthor = {
    id        : this.id,
    firstName : this.firstName,
    lastName  : this.lastName,
    email     : this.email,
    phone     : this.phone,
    address   : this.address,
 };
var newAuthor = {
    id        : this.id(),
    firstName : this.firstName(),
    lastName  : this.lastName(),
    email     : this.email(),
    phone     : this.phone(),
    address   : this.address(),
};