Hello Guys,
Just wondering what's the meaning of ":" (colon symbol) on this Javascript code below?
var switchToTarget : Transform;
Thanks, Gino
Hello Guys,
Just wondering what's the meaning of ":" (colon symbol) on this Javascript code below?
var switchToTarget : Transform;
Thanks, Gino
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;
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:
The object literal syntax:
var obj = { foo: 'bar' };
The conditional operator:
var test = condition ? 'foo' : 'bar';
loop1: while (true) {
while (true) {
break loop1; // stop outer loop
}
}
Case and default clauses of the switch statement:
switch (value) {
case "foo":
//..
break;
default:
//..
break;
}
It can appear on RegExp literals:
var re = /(?:)/; // non-capturing group...
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