Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Classes and prototypes ?

ANSWERED

Hello,

 

I've built a first version of my clockface with the 4.0 SDK recently and I was using classes with prototypes and I updated to the 4.1 SDK 3 days ago and for some reasons I can't compile anymore.

 

I get " App: Unhandled exception: TypeError: Expected a function. " whenever I try to call a prototype function in my classes.

 

Does anyone have any ideas as to why this is happening ?

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

Ok, I've figured it out.

 

Basically it's my fault, I was calling the "this" keyword inside another class that did not have the method I was trying to execute. The call was mixed up inside a class that add a single call to a system class which explains the mix up !

 

Eveything is working fine now !

View best answer in original post

Best Answer
0 Votes
6 REPLIES 6

Do you have some code that worked before which now doesn't we could look at? It's hard to help without specifics unfortunately.

Best Answer
0 Votes

Yes sure :

 

Here's a sample class that used to work and does not anymore :

 

export function RetroSettings(){
    this.settings_type = "json";
    this.settings_file = "settings.json";
}

RetroSettings.prototype.load = function() {
    this.settings = this.loadSettings();
    //console.log(`Settings loaded : ${JSON.stringify(this.settings.classe)}`);
    this.applyTheme(this.settings.background, this.settings.foreground);
    this.applyCharacter(this.settings.classe);
    this.applyClockSize(this.settings.size);

    // Listen for the onmessage event
    messaging.peerSocket.onmessage = evt => {
        //console.log(`App received: ${JSON.stringify(evt.data)}`);
        if(evt.data.classe){
            this.applyCharacter(evt.data.classe);
        }
        if(evt.data.background && evt.data.foreground){
            this.applyTheme(evt.data.background, evt.data.foreground);
        }
        if(evt.data.size){
            this.applyClockSize(evt.data.size);
        }
    };
    // Register for the unload event
    appbit.onunload = this.saveSettings;
};

The loadSettings method throws this error now : 

 

App: Unhandled exception: TypeError: Expected a function. ? at app/retro/RetroSettings.js:28,1

 

This is just and example but every prototype methods throw this error now.

 

Works on SDK 4.0 but not 4.1

Best Answer
0 Votes

Is that the whole file? Just trying to figure out which line the error refers to.

Best Answer
0 Votes

I just tried this code in both SDK 4.0 and SDK 4.1 and saw both log messages with no errors:

function MyClass() {
  console.log('ctor');
}

MyClass.prototype.load = function() {
  console.log('load');
}

const mc = new MyClass();
mc.load();

So I think your example in isolation perhaps isn't enough to reproduce whatever the issue is.

 

Best Answer
0 Votes

Hi,

 

I'm wondering if it comes from the "this" keyword inside the class itself for some reason.

 

Calling the method outside works but calling it from inside the class does not...

 

Weird...

Best Answer
0 Votes

Ok, I've figured it out.

 

Basically it's my fault, I was calling the "this" keyword inside another class that did not have the method I was trying to execute. The call was mixed up inside a class that add a single call to a system class which explains the mix up !

 

Eveything is working fine now !

Best Answer
0 Votes