未定义不是这个和自我的功能用途

时间:2016-03-07 10:42:56

标签: javascript

当执行以下ViewModel函数时,错误 Uncaught TypeError:self.deleteInvisibleColumns不是函数被引发 - 为什么会这样?

function ViewModel(vm) {
    var self = this;

    this.rows = ko.observableArray(vm.Rows);
    self.deleteInvisibleColumns();

    this.deleteInvisibleColumns = function () {
        for (var i = 0; i < vm.Rows.length; i++) {
            var row = vm.Rows[i];

            var keys = Object.keys(row);
            for (var k = 0; k < keys.length; k++) {
                if (vm.VisibleColumns.indexOf(keys[k]) === (-1)) {
                    delete row[keys[k]];
                };
            };
        };
    };

1 个答案:

答案 0 :(得分:1)

试试这个:

  1. 在callint之前删除函数定义

  2. 对函数定义使用“self”并调用它

    function ViewModel(vm) {
    
        var self = this;
    
        this.rows = ko.observableArray(vm.Rows);
    
        self.deleteInvisibleColumns = function () {
            for (var i = 0; i < vm.Rows.length; i++) {
               var row = vm.Rows[i];
    
               var keys = Object.keys(row);
               for (var k = 0; k < keys.length; k++) {
                  if (vm.VisibleColumns.indexOf(keys[k]) === (-1)) {
                     delete row[keys[k]];
                  };
              };
          };
        };
    
        self.deleteInvisibleColumns();
    };