如何在javascript中返回key:function()的值

时间:2013-10-01 21:33:13

标签: javascript backbone.js

以下是骨干模型中的内容:

defaults: {
    prayer_string: function (){
            var label_val = $("#prayer_type_label").val();
            console.log("Prayer_string returning: ", label_val);
            return label_val;
    }
}

但是当我像这样访问模型时:

var prayerString = model.prayer_string;

prayString是函数而不是返回label_val。

我需要做什么才能获得函数的返回值而不是函数本身?

谢谢,

安德鲁

1 个答案:

答案 0 :(得分:0)

请改为尝试:

var yourModel = new (Backbone.Model.extend({
    prayer_string: function () {
        var label_val = $('#prayer_type_label').val();
        console.log('Prayer_string returning: ' + label_val); // use + for concatenating strings
        return label_val;
    }
});

var prayerString = yourModel.prayer_string(); // invoke the method with ()