How to bypass adding protoype word to every variable and method

Currently I am having to add the word ‘prototype’ to every variables and method in my script:

w18.prototype.doThing = function() blah blah...

I tried doing:

w18.prototype = {
    doThing: function() blah blah...
};

But now I’m getting this error:

this.scripts[i].__initializeAttributes is not a function

I also tried:

w18.prototype += {

}

But I get the same error. Any suggestions?

You can use ES6 syntax, e.g.:

class MyScript extends pc.ScriptType {
    
    initialize() {
        this.total = 1;
        const five = 5;
        this.addFive(five);
    }
    
    addFive(val) {
        this.total += val;   // 6
    }
}
pc.registerScript(MyScript, 'myScript');
1 Like

Perfect! Thank you so much @LeXXik!

Why don’t they create the examples using ES6 syntax?

Not all browsers support ES6. e.g. IE11 is still supported by PlayCanvas and only supports up to ES5.

The publish process is still a straightforward concatenate and minify. We are thinking about improving this but not made plans yet.

1 Like

I’d say thats a pretty good reason. Thanks for the explanation.