views:

101

answers:

3

Hello Guys,

Just wondering what's the meaning of ":" (colon symbol) on this Javascript code below?

var switchToTarget : Transform;

Thanks, Gino

+1  A: 

I'm not sure if it's part of standard JavaScript, but it declares the type of a variable.

var myVar:Type;

in that flavor of JavaScript would be equivalent to this in several strongly-typed languages:

Type myVar;
icktoofay
+6  A: 

Edit: Reading more about Unity, they have created a really custom implementation of JavaScript(1) for their scripting engine, which is compiled and it has a lot of strongly typing features, it looks like ActionScript/ES4, but it isn't, the language is called UnityScript.

The colon is used by this implementation to denote the type of an identifier, e.g.:

class Person{
   var name : String;
   function Person(n : String){
      name = n;
   }
   function kiss(p : Person){
      Debug.Log(name + " kissed " +  p.name + "!");
   }
}

See also:


The code you posted is not valid ECMAScript 3, (which is the most widely implemented standard), that will simply give you a SyntaxError.

The colon symbol in JavaScript has only a few usages:

  1. The object literal syntax:

    var obj = { foo: 'bar' };
    
  2. The conditional operator:

    var test = condition ? 'foo' : 'bar';
    
  3. Labeled statements:

    loop1: while (true) {
      while (true) {
        break loop1; // stop outer loop
      }
    }
    
  4. Case and default clauses of the switch statement:

    switch (value) {
      case "foo":
        //..
      break;
      default:
        //..
      break;
    }
    
  5. It can appear on RegExp literals:

    var re = /(?:)/; // non-capturing group...
    
CMS
What about regex literals? ;)
Matthew Flaschen
@Matthew: Yes, also the `case`/`default` clauses of the `switch` statement :)
CMS
Then you also need to mention “string litarals” and “comment literals”. ;-)
Gumbo
+4  A: 

It's Adobe ActionScript, which is a derivative of javascript.

var switchToTarget : Transform; // declare var switchToTarget of type Transform.

var hello : Text = new Text(); // declare var hello of type Text and initialize it.

http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/geom/Transform.html

Blessed Geek
I'd say it's more a derivative of ECMAScript.
alex