hot to make classes and why ?

how can i make class inside JS file ? and why i had to make a class ? i used to work without making classes from several years but i found i had to know little about that instead of ignoring it. i need to know the main form of it & a clear example , because i found lots of answers but it makes me confused little a bit .

thanks
Muhammad

1 Answer

1

Well, for starters, every JS file you make is a class. The compiler automatically wraps the content of the file in the correct bits to make it work.

If you want to create a seperate class within that (for example, if you want to organise variables in a more sensible fashion), you use this syntax-

class MyClassName{
    var anInteger : int;
    var aBoolean : boolean;
    var aFloat : float;

    function DoSomething : float
    {
        return aFloat + anInteger;
    }
}

Then, to use this in your script, make an instance like this-

var anObject : MyClassName;