Forum Discussion

fahlen's avatar
fahlen
Occasional Contributor
15 years ago

New behavior for JScript Object prototype property

When using TestComplete 7.20, I extended arrays, numbers and strings with new functionality as shown in the following.



Array.prototype.contains = function(element)

{

  for (var i=0; i<this["length"]; i++)

  {

    if (this == element)

    {

      return true;

    }

  }

  return false;

};



Number.prototype.toFixed = function(precision)

{

  var power = Math["pow"](10, precision || 0);

  return String(Math["round"](this * power) / power);

};



String.prototype.repeat = function(n)

{

  return new Array(n + 1)["join"](this);

};





This worked well. However, now that I'm using TestComplete 7.52, I keep getting exceptions stating:



Microsoft JScript runtime error

Object doesn't support this property or method



I use C#Script, but as I understand it, this uses an underlying JScript engine. My questions is this. Has any changes been made that would prevent me from extending arrays, numbers and strings with new methods? Will this be a no no, from now on? This worked fine before. It would be great if anyone could shed some light on the matter.

15 Replies


  • Hi David,





    I'm not sure what you mean here. Even after a few syntax corrections, the code you provided does not work on my machine. Can you give me a complete example (not just prototype declarations) which works for you?


  • I've attached a small project suite that I created. There are 5 script files named "Person", "Employee", "xPerson", "xEmployee" and "xManager". Each scritp contains a simple test function that creates an object and writes the contents of its "toString" function to the log. The inheritance hierarchy is as such:



    Person

      Employee   - Employee's toString function calls Person's toString function.

        Manager   - Manager's toString function calls Employee's toString function.



    I've also included the log files from each test run. The first three logs are from executing the test functions of xPerson, xEmployee and xManager, respectively. All worked as intended.



    I then defined script objects "Person" and "Employee". The contents of these scripts are identical to the contents of "xPerson" and "xEmployee" except that I changed the strings printed to the log to make them easier to identify.



    I then ran the test function from "Person", no problems, none expected.



    I then ran the test function from "Employee", but in this case the first line in the script was "//USEUNIT Person". This generated the runtime error "Object doesn't suppport this property or method" indicated in the log.



    In script "Employee", I changed the first line to "//USEUNIT xPerson" and executed the test function again. This time the execution was correct.



    My installation of TestComplete includes no plug-ins or extensions that didn't come with the original installation file.



    So, to make JavaScript prototypal inheritance work in TestComplete, give the script modules slightly different names than the object being defined.



    Dave

  • Hi Dave,





    Thanks for the sample project. There seem to be a problem in our documentation regarding prototypes. We'll perform more investigations on this and will consider changing the documentation accordingly.


  • Please fix the documentation, just don't "fix" the functionality. I can live with having to name my script objects "mod_xyz", but if object inheritance was broken, I'd be very upset.

  • Hi Dave,





    Our investigation shows that the limitation currently described in the documentation (inability to use prototype methods in other units) applies only to standard objects (like Array instances), but does not apply to custom objects, like those in the sample you provided. We will update the documentation accordingly.