Forum Discussion

Nick_Tulett's avatar
Nick_Tulett
New Contributor
14 years ago

Support for prototypal inheritance in JScript broken in 7.52

I'd like to write a script in JScript that uses prototypal inheritance, such as:



Object["create"] = function (o) {  

    function F() {}  

    F["prototype"] = o;  

    return new F();  

};



function BaseObject(name) {

  this["name"] = name;

}

BaseObject["prototype"] = {

  writeName: function () {

    Log["Message"](this["name"]);

  },

  changeName: function (newName) {

    this["name"] = newName;

  }

}



function NobleObject(name) {

  BaseObject["call"](this, name);

  this["rank"] = "noble";

}

NobleObject["prototype"] = Object["create"](BaseObject["prototype"]);

NobleObject["prototype"]["changeRank"] = function (newRank) {

  this["rank"] = newRank;

}

NobleObject["prototype"]["writeRank"] = function () {

  Log["Message"](this["rank"]);

}



function testObjects() {

  myBase = new BaseObject("testBO");

  myBase["writeName"]();

  myBase["changeName"]("newTestBO");

  myBase["writeName"]();       

 

  myNoble = new NobleObject("testNO");

  myNoble["writeName"]();

  myNoble["writeRank"]();

  myNoble["changeName"]("newTestNO");

  myNoble["writeName"]();       

  myNoble["changeRank"]("tarnished");

  myNoble["writeRank"]();       

}



This is valid JScript and runs on TC 5 but TC 7.52 flags the changeRank method as being a duplicate method name (presumably it's confused by the "prototype" call). Could we please remove this check from JScript scripts as it is broken?



In a similar vein, the Code Explorer only shows the "constructor" functions and not any of the prototype functions. Can this be fixed, too?

3 Replies

  • It looks like you're a fan of Douglas Crockford's work.  May I ask why you're using subscript notation?  Have you tried using prototype in dot notation?  Maybe that's where it's breaking.  We're using a library that uses prototypal inheritance and it's working fine in TC 7.52.


  • Thanks, it does seem to work with dot notation. Though I still consider the editor to be broken as this is a regression from v5.



    I prefer subscript as it allows me to see my method names more easily in the editor and on the rare occasion that I actually need to parameterise them, it makes it trivial.



    And, yes, Crock is the man.





    Another question I meant to ask AQA was whether I can replace the creaky old JScript engine with something a bit more modern, such as SpiderMonkey or V8?
  • I use subscript notation for a bit of metaprogramming so you can continue to use it, just don't use it when you call prototype:



    e.g., NobleObject.prototype
    = Object["create"](BaseObject.prototype);



    It should still work with that change.



    As for the JScript replacement for SpiderMonkey or V8, I think TestComplete is pretty much married with (Microsoft) JScript.  Any change to the engine could break backwards compatibility, but still a good question to ask AQA.



    AQA: Can JScript engine be replaced in the foreseeable future?