Learning Javascript. Need some explanations please

Hello everyone,

I just downloaded Unity and people have told me if I want to make effective use of it I need to learn the basics of Javascript or C#. So I looked around to see which language would be best to learn and after reading through several posts here and websites describing the benefits of each I decided on Javascript (by coin toss actually). I have very basic programming experience from several years ago (C++) but basically gave up on it since the instructors ,while awesome programmers, were terrible teachers and most of what they said went over my head.

I have been going through some tutorials for people who have no experience programming in order to get a good foundation with Javascript but there is a common problem with all of them, none of them stop to explain how try their examples or why they are doing what they are doing in each example. This problem begins just after the “hello world” example and right now I’m trying to wrap my head around the concept of classes.

I’m at chapter 2 of the guide at the unify wiki → http://www.unifycommunity.com/wiki/index.php?title=Programming_Chapter_2_Old. Around the middle of the page is this example;

// File: Point.js
// Have to declare as class to make a constructor
class Point
{
    var x : int;
    var y : int = 5; // We can initialize here too, setting a default value if none is supplied
   
    // Empty constructor, initialize point to [1, 1]
    function Point()
    {
        x = 1;
        y = 1;
    }
   
    // Constructor to let user set initial point
    function Point(ix, iy)
    {
        x = ix;
        y = iy;
    }
   
    // Move from current position the requested distance
    function Translate(dx, dy)
    {
        x += dx;
        y += dy;
    }
}

Before this is a brief note that classes need a “constructor” but I can’t find anything about what the constructor is or why we need it. Then in the comments for the code there is another “constructor” for the 2nd function in the class.

So basically, I’m trying to figure out what a constructor is and in this example why two nearly identical functions are necessary to make the class work properly. Also, can anybody recommend any other tutorials out there? I saw tonyd’s post and I appreciate the effort but it’s less helpful than the unify wiki. Thanks a ton!

A constructor is a function that runs when the class is initialized. You don’t necessarily need it, depending on what you’re doing. If nothing else, it can make using the class more convenient.

class Foo {
   var a : int;
   var b : float;
}

With that, you’d do this:

var blah = new Foo();
blah.a = 5;
blah.b = 4.7;

With a constructor:

class Foo {
   var a : int;
   var b : float;

   function Foo (anInteger : int, aFloat : float) {
      a = anInteger;
      b = aFloat;
   }
}

you can do:

var blah = new Foo (5, 4.7);

which makes initializing the variables simpler.

Of course, that’s a trivial example, and you can use constructors to do much more complex setup than just assigning variables.

–Eric

Thanks that helps a little. I guess I need an example that does more than instantiates the class to change a variable. Right now all that the constructor looks like it does is changes your var a to anInteger, which again makes me wonder why you would need it. Sorry for running in circles.

It’s hard to ask programming questions because if you accidentally use a word that has a different meaning in the language you end up with an answer that confuses or tricks you. T~T

Well, but I already explained that…without the constructor, you have to do this:

var blah = new Foo();
blah.a = 5;
blah.b = 4.7;

With the constructor, you can just do this:

var blah = new Foo (5, 4.7);

The constructor initializes the variables. A slightly more involved example:

class Foo {
   var vector2Array : Vector2[];
   var vector3Array : Vector3[];
   
   function Foo (v2Size : int, v3Size : int) {
      vector2Array = new Vector2[v2Size];
      for (v in vector2Array) v = Vector2.one;
      vector3Array = new Vector3[v3Size];
      for (v in vector3Array) v = Vector3.one;
   }
}

Then you can do this:

var blah = new Foo (5, 8);

This will result in blah.vector2Array being initialized to 5 entries, where each one is Vector2(1,1), and blah.vector3Array being initialized to 8 entries, where each one is Vector3(1,1,1).

–Eric

Not an expert but:

How else would you set ‘a’ to ‘anInteger’?

As above this way is nice and easy.

Secondly its a function - you can run pretty much any code you want in it IIRC. If you need to ensure something exists, register it with something else…

A completely imaginary way-point system(sorry for syntax)…

//gets myID to return to creator.  0 = failure
var myID : integer = 0;

function WayPoint(position : Vector3, ownerID : short){

//Check to see if current position is valid free position.  Maybe the sun is a bad place to try to go to.
If (getPositionStatus(position))
{
   //Register with way-point manager (different owners have different waypoints
   var myID : integer = registerWaypoint(ownerID);
}
}

VB Hangman server (not unity). Reads in dictionary and sets up my refresh system.

    Public Sub New()
        'Read in Dictionary
        DictionaryRefresh()
        'Set Up 
        Dim _timDictionary As New Timer
        AddHandler _timDictionary.Tick, AddressOf DictionaryRefresh
        _timDictionary.Interval() = 1000
        _timDictionary.Start()
    End Sub

Ok, I think I get it now. It will probably start clicking when I’m able to remember the syntax and write basic programs. Thanks guys!