4

I am getting the error 'TabsViewModel is not a constructor' with the following code. I've looked at other posts regarding this error, but most suggest that the function name is in use somewhere else, but as you can see this is a very simple example and TabsViewModel is not used elsewhere.

        define(['utils','ko','text!tmpl/risersandfallerstabs_partial.html','text!tmpl/errorPage_partial.html'], function(utils,ko,tmplrisersandfallerstabsStr,tmplErrStr) {
let tabsmodel = {}; 
$(function() {           
    tabsmodel = new TabsViewModel();
    ko.components.register('risersandfallerstabs', {
        viewModel: utils.createVM(tabsmodel), 
        template: tmplrisersandfallerstabsStr                      
    });
    utils.applyBindings(tabsmodel,'risersandfallerstabs');

    var TabsViewModel = (function(){
        let self = {};
        self.test = "123";
        return self;
    });
});
})

Why am I getting this error?

2 Answers 2

5

You're trying to call TabsViewModel before it exists. Move your function definition above the function execution.

  var TabsViewModel = (function() {
    let self = {};
    self.test = "123";
    return self;
  });

  tabsmodel = new TabsViewModel();
0
1

I was running into a similar issue, the problem was that my class was an anonymous function instead of a normal function.

Didn't work:

var TabsViewModel = () => { ... }

Worked:

var TabsViewModel = function() { ... }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.