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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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