- Home /
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 ()
{...}
3) 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!
one small point. Note that with Unityscript.
Say you make a file called "happy.js"
IN FACT, it AUTO$$anonymous$$ATICALLY makes that a Class - even if you don't have a Class blah{ } inside it wrapping everything. Really it's quite confusing. It's one of those things that is simple but a bt confusing if you think on it too much :)
Regarding the deep question "What is a Class?" Nobody has a clue. Your definition is as good as anyone elses. When people try to talk about it epistemologically, they just get in to knots saying it is "an object" or something. If you read the books on deep OO theory, they're pretty simplistic to any working philosopher. So I'd totally forget that line of enquiry. :)
I really urge you just to USE THE$$anonymous$$ A LOT and then you will understand perfectly.
A really important word to use to sound important is:
INSTANCE
What you have above is a Class DEFINITION
when you do this,
var PlayerTimer : TimerClass = new TimerClass ( ) ;
you ARE ACTUALLY $$anonymous$$A$$anonymous$$ING ONE OF THE THINGS. (You've made a TimerClass "thingy" - so you get to use the fancy word "instance", You've "made an instance" of that Class.)
Enjoy!
comment converted to answer
Fattie actually summerized it all up first with IN FACT, it AUTO$$anonymous$$ATICALLY makes that a Class - even if you don't have a Class blah{ } inside it wrapping everything.
That is what I was trying to explain by expanding how the JS script really is automatically a class.
Thanks @alucardj & @Fattie , after both your comments, I totally have no questions about it! Thank you so much!
but i have one issue, so how do I mark it as answered? since both are only comments :D ( I have thumbed both up).
I think I will just post an answer, and say please see the comments below the questions :D If not, please let me know what I should do it more appropriately.
Ask whoever you want to accept his answer, to convert his comment to answer so that you can accept it :-)
Answer by AlucardJay · Nov 02, 2012 at 01:18 PM
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 =]
Hi
I understand all of this post except for the " var myTorch : FlashlightScript = new FlashlightScript(); " part.....
Why is there a 'new' script being declared in the variable? What does it do or what's its significance?
Thanks
The flashlight discussion was carried over from the OPs other question : http://answers.unity3d.com/questions/340976/pause-unpause-different-timers.html
As you say, it wouldn't be done to store a reference to a script, but above it is used just to reiterate how to declare and assign a new class to a variable.
Your answer
Follow this Question
Related Questions
How to Typecast JS Variables as C# Classes? 0 Answers
Enum style variable 2 Answers
Is it possible to create a script dinamic like animation>size controling the Elements variables? 1 Answer
Different way to access class variables? 1 Answer
Creating A Class Variable That Works With Any Class 0 Answers