Jump to content

Classes


rarebit

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/131235-classes/
Share on other sites

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...

Link to comment
https://forums.phpfreaks.com/topic/131235-classes/#findComment-681376
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.