• 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
    >
    

    1 Attachment

    • Screen Shot 2016-05-06 at 8.38.46 AM.png
About

Avatar for allObjects @allObjects started