Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the meaning of ":" (colon symbol) on this Javascript code "var switchToTarget : Transform;"?

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

var switchToTarget : Transform;

Thanks, Gino

like image 390
steamboy Avatar asked Jun 23 '10 04:06

steamboy


Video Answer


2 Answers

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:

  • UnityScript Reference
  • Head First into Unity with JavaScript
  • Scripting Overview
  • Unity Answers

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...
    
like image 86
Christian C. Salvadó Avatar answered Sep 28 '22 13:09

Christian C. Salvadó


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

like image 38
Blessed Geek Avatar answered Sep 28 '22 11:09

Blessed Geek