Javascript错误 - 对象没有方法

时间:2012-07-16 20:47:45

标签: javascript jquery object

我缩短了以下脚本:

var HTH = HTH || {};

(function() {
    var assetGrouping = function() {
        var self = this;

        this.options = {
            _tmpElement:        '',
            QuantityAssigned:   0,
            qtyInputField:      ''
        };

        this.init = function(options){
            // ...
            this.options.QuantityAssigned = 0;
            jQuery(this.options.qtyInputField).bind('keyup', function(){
                self._tmpElement = jQuery(this);
                self.CalculateQuantityAssigned();
            });

            // ...
        }

        CalculateQuantityAssigned = function(){
            // ...
        }
    }

    HTH.assetGrouping = new assetGrouping();
})();

$(document).ready(function(){
    HTH.assetGrouping.init({
        qtyInputField: 'input[name^="at700_group_qty"]'
    });
});

错误发生在以下行:self.CalculateQuantityAssigned();,错误为Uncaught TypeError: Object [object Object] has no method 'CalculateQuantityAssigned'

我不明白。使用this当然会失败,self在我想要访问self.options而不是self.CalculateQuantityAssigned()时有效。

感谢。

2 个答案:

答案 0 :(得分:5)

变化:

CalculateQuantityAssigned = function(){
            // ...
}

this.CalculateQuantityAssigned = function(){
            // ...
}

答案 1 :(得分:1)

(function () {
});  <-- You have a function, but you never execute it!

您需要添加();