rarebit Posted November 3, 2008 Share Posted November 3, 2008 Hi, just trying a few variations, but for some reason the first output of the following gives two 'undefined', any reason why, I thought you could have two (or more) constructors like this? <script type="text/javascript"> /* 4. */ function myclass() { this.a = 'Hello'; this.b = 'World'; } function myclass(a,b) { this.a = a; this.b = b; } var c = new myclass(); document.write('4a. '+c.a+' '+c.b+'!<br>'); var c = new myclass('World','Hello'); document.write('4b. '+c.a+' '+c.b+'!<br>'); </script> Quote Link to comment Share on other sites More sharing options...
Mchl Posted November 3, 2008 Share Posted November 3, 2008 JavaScript does not support overloading of classes AFAIK. Quote Link to comment Share on other sites More sharing options...
rarebit Posted November 3, 2008 Author Share Posted November 3, 2008 In my tests i'd be inclined to agree with you. This is a quote from here: http://www.developertutorials.com/tutorials/javascript/writing-classes-in-javascript-050404/page1.html Definition of a class - Definition of a class in JavaScript is done by just defining a no-parameter constructor. you can have constructors taking parameters but one constructor which doesn't take any parameter is mandatory. The name of the class is the name of the constructor. oh well, no matter... Quote Link to comment Share on other sites More sharing options...
Mchl Posted November 3, 2008 Share Posted November 3, 2008 The wording is somewhat strange... Probably what they mean, is that class is defined by constructor, so you must define a constructor even if it takes no parameters... Weird way to put it... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.