Hi,
I am trying to learn object oriented javascript and I ran into a problem, which I can't understand. To keep it simple I created a dummy object and then placed everything into one html file, which is below:
If you try to run this html file, it will create an object of ObjectTest. In the constructor,HTML Code:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Object Test</title> <script type="text/javascript"> function ObjectTest() { var height ; //CONSTRUCTOR BEGIN this.setHeight(250) ; //CONSTRUCTOR END //PREVILIGED FUNCTION BEGIN this.setHeight = function(aHeight) { height = aHeight ; alert(height) ; } //PREVILEGED FUNCTION END } </script> </head> <body> <script type="text/javascript"> var myObj = new ObjectTest() ; //myObj.setHeight(250) ; </script> </body> </html>line is giving the actual error. I get an error that says this.setHeight is not a function.Code JavaScript:this.setHeight(250) ;
But if you comment that line and uncommentinside the body tag, then, everything works fine.Code JavaScript:myObj.setHeight(250) ;
My question is how do I call previleged methods such as setHeight() from the constructor of the javascript object.
Thank you so much in advance for your help.




Bookmarks