Forum Discussion

sergi's avatar
sergi
Contributor
11 years ago

Share Function.prototype between Units

Hi,

I'm having problems sharing the prototype functions between different units.



I've read this:

http://smartbear.com/forums/f74/fp167/t49620/jscript-exception-propagation-between-script-un/



and this:

http://support.smartbear.com/viewarticle/57893/?_ga=%221.165320700.233964626.1367322859%22#JS



which seem to be related, but I can't figure out how to solve my particular case.



I have the following which is not working.




----------------------------


FunctionUnit


----------------------------


 


Function.prototype.inheritsFrom = function( parentClassOrObject ){ 


if ( parentClassOrObject.constructor == Function ) 




//Normal Inheritance 


this.prototype = new parentClassOrObject;


this.prototype.constructor = this;


this.prototype.parent = parentClassOrObject.prototype;




else 




//Pure Virtual Inheritance 


this.prototype = parentClassOrObject;


this.prototype.constructor = this;


this.prototype.parent = parentClassOrObject;




return this;


}


 


----------------------------


TestUnit


----------------------------


//USEUNIT FunctionUnit


 


function MyClassA(){}


 


MyClassB.inheritsFrom(MyClassB);


function MyClassB(){}





Any suggestions?



Thanks



  • Philip_Baird's avatar
    Philip_Baird
    Community Expert

    Hi Sergi, unfortunately prototype augmentation only applies to the Script Unit in which it is declared.


     


    From what I have observed, each Script Unit runs in it's own "sandbox" which puts in place this restriction.


     


    I have had success in the pass using Factory functions in relation to augmented prototypes, e.g.


     


    // Factory function


    function AiArray(){


      if( arguments.length === 1 ){


        return new Array().concat( arguments[ 0 ] );


      }


      return new Array()


    }


     


    // Augment Array


    Array.prototype.clear = function() {


      if( ! this.isEmpty() ) {


        this.splice( 0, this.length );


      }


    }


     


    However, since you are implementing an inheritance function this may not be efficient.



    As an aside, I use Base.js for inheritance, so far this has worked well for me in Test Complete.




    Regards,


    Phil Baird




  • Ryan_Moran's avatar
    Ryan_Moran
    Valued Contributor
    Doesn't work for me either. Had to add prototype modifications to top level unit/any units that need to utilize them.