Raymond Ferguso
Tera Contributor

UNEDIT: The first version didn't work in all cases. I found my bug elsewhere and reverted. The first version works fine.

It's pretty easy to find the pitfalls of Object.extendsObject when trying to write DRY serverside RhinoJS code. Setters and getters are one example.  The code below demonstrates the problem and a solution.

What are others doing? And more importantly, does anyone know why Object.extendsObject was implemented with a faulty Object.clone and they avoided prototype chaining in the first place?

I'm guessing that prototype chaining is just inefficient or not implemented properly in rhino, and maybe Object.clone was working just fine back in ES4 when there were less features?

It would be nice to get some conformation though lest I wake a crusty rhino dragon with extendClassy.


var extendClassy = function(parentClass, childPrototype) {
    /* Same as Object.extendsObject, returns child prototype, but
     * with better comaptibility for various property types. */
    var rproto = (gs.nil(childPrototype)) ? {} : childPrototype;
    for (var property in parentClass.prototype) {
        if (gs.nil(Object.getOwnPropertyDescriptor(rproto, property))) {
            Object.defineProperty(rproto, property,
                Object.getOwnPropertyDescriptor(parentClass.prototype, property)
            );
        }
    }
    return rproto;
};

var asdf = Class.create();
asdf.prototype={
  initialize: function(s){
    this._v=s
  },
  get value(){
    return this._v ;
  },
  type: "asdf"
};

var ClassyExtended = Class.create();
ClassyExtended.prototype = extendClassy(asdf,{type: "ClassyExtended"});

var DumbExtended = Class.create();
DumbExtended.prototype = Object.extendsObject(asdf,{type: "DumbExtended"});

gs.print( (new ClassyExtended("hello world")).value ); 
// -> hello world

gs.print( (new DumbExtended("hello world")).value );
// -> undefined
Version history
Last update:
‎08-07-2019 09:05 AM
Updated by: