Declaring & understanding Class in javascript

Hi Guys,

I wish to ask 2 questions about declare class and the concept of it, it also known as typecasting. The code is in javascript (unityscript)

From my own understanding, that class in javascript or unityscript is like having an array contain many items or having one big variable contains many small variables.

Example, example would be like Ball is a class has few properties ( a few variables, the weight, diameter & material used, condition of the ball; new or used)

  1. Is my understanding of it correct? If not what would the more appropriate example of looking at it.

  2. Assume my own understanding of it is correct, what is the proper or long term of defining class?

    var PlayerTimer : TimerClass = new TimerClass ( ) ;
    or
    var PlayerTimer = new TimerClass ( ) ;

Below is my actual code:

class TimerClass 
{
    var FirstTimer : float = 0 ;
    var FirstTimerActive : boolean = true ;
    var SecondTimer : float = 0 ;
    var SecondTimerActive = true ;
 }
    
    var PlayerTimer : TimerClass ; // THIS IS THE ONE
 function Update ( )
 {..}

 function OnGUI ()
 {...}
  1. Lets say for this one “var PlayerTimer : TimerClass = new TimerClass ( ) ;” I don’t actually understand the statement of it.
    I tried to come out with my own understanding,
    like this:

    class BallCLASS
    {
    var condition
    var weight
    var diameter of the ball
    var price
    }

    var JohnSpecialBaseball : BallClass = new BallClass ():
    var NormalBaseBall : BallClass = new BallClass ():
    var BowlingBall : BallClass = new BallClass ();
    var (A) : (B) = ( C)//for the asking and explaining below only

So to me it’s like var a name or a certain ball (A) is actually a BallClass(B) type which is actually a NEW BallClass (C), which naturally (A) has the property of (B) since it’s a new (C)

could I look at it this way?
This question is more like asking how a language statement is actually form with its noun and grammar etc.

So is my own understanding of declaring PROPERLY & LONG way of class correct? Please let me know if there’s any more information I would need to know to understand everything about it, I’m still learning unity and really curious about it.

Thanks in advance for all the explanations!

Disclaimer : this is written by a noob, with just a little more than a dangerous amount of knowledge =]

Classes

The easiest way to imagine it is … a class is a script.

If you create a new C# script, you’ll see the format :

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {
    // Use this for initialization
    void Start () {
    }
    
    // Update is called once per frame
    void Update () {
    }
}

If you create a JS, this is not seen :

#pragma strict

function Start () {
}

function Update () {
}

but it is actually automatically set like :

#pragma strict

public class NewBehaviourScriptJS extends MonoBehaviour {

    function Start () {
    }
    
    function Update () {
    }
}

Now when I say a class is a script, a script has variables :

#pragma strict

var timer1 : float = 0;

function Start () {
}

so this would be the the class :

#pragma strict

public class NewBehaviourScriptJS extends MonoBehaviour {

    var timer1 : float = 0;
    
    function Start () {
    }
}

with your timers question, the class is just written into the main class/script :

#pragma strict

public class NewBehaviourScriptJS extends MonoBehaviour {

    class Timers {
        var timerCounter : float = 0;
        var timerMax : float = 5.5;
    }
    
    var timer1 : Timers = new Timers();
    
    function Start () {
    }
}

Types and Typecasting

so if I call timer1.timerMax , this means I am calling variable timer1 (of type Timers) and within that class I am calling the variable timerMax (of type float).

Now looking back to your other qn with my flashlight example, this may make more sense thinking of how a class is a script (in this example).

I said if you have a GameObject, and attach a script to it (called FlashlightScript), then to be able to read the variables can call functions on this script, you can do :

var myTorch : FlashlightScript;

This means I am declaring variable myTorch of type FlashlightScript.

just like a class, then the variables of the script can be called with

 myTorch.someBoolean = true; 
 var bl = myTorch.batteryLifeRemaining;

in this class example, this could be written as :

#pragma strict

public class PlayerScript extends MonoBehaviour {

    class FlashlightScript {
        var someBoolean : boolean = false;
        var batteryLifeRemaining : float = 5.5;
    }
    
    var myTorch : FlashlightScript = new FlashlightScript();
    
    function Start () {
    }
}

I have probably confused the issue for you, but I am sure the gurus on this 'site will be happy to correct me and help you with follow up questions =]

Edit : Am simply glad my ravings helped in some way !

Happy Coding =]