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

StackOverflow https://stackoverflow.com/questions/3098979

  •  29-09-2019
  •  | 
  •  

Pergunta

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

var switchToTarget : Transform;

Thanks, Gino

Foi útil?

Solução

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...
    

Outras dicas

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

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;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top