Code using deprecated __proto__ (see screenshot) works:
var Obj = function(val) {
this.val = (val) ? val : this.__proto__.defaultVal;
};
Obj.prototype.defaultVal = "defaultVal";
Obj.prototype.getVal = function() {
return this.val;
};
var objDefault = new Obj();
console.log("val = " + objDefault.getVal());
Console output:
1v85 Copyright 2016 G.Williams
>echo(0);
val = defaultVal
=undefined
>
Code 'correctly' getting prototype:
...
this.val = (val) ? val : Object.getPrototypeOf(this).defaultVal; // line 4
...
Console output:
1v85 Copyright 2016 G.Williams
>echo(0);
Uncaught Error: Function "getPrototypeOf" not found!
at line 4 col 33
...val = (val) ? val : Object.getPrototypeOf(Obj).defaultVal;...
^
in function "Obj" called from line 1 col 26
var objDefault = new Obj();
^
val =
Uncaught Error: Field or method "getVal" does not already exist, and can't create it on undefined
at line 1 col 34
console.log("val = " + objDefault.getVal());
^
=undefined
>
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Code using deprecated
__proto__
(see screenshot) works:Console output:
Code 'correctly' getting prototype:
Console output:
1 Attachment