colon in variable declaration

2019-05-26 08:47发布

问题:

I'm going through some Unity tutorials and came across this line of code in one of the sample scripts. I'm familiar with javascript but I have never seen the colon used when declaring a variable except for object literals.

var controller : CharacterController = GetComponent(CharacterController);

What is the colon doing in this line.

at the end of the script there is this other line of code that might be relevant

@script RequireComponent(CharacterController)

Here is the full sample code from unity:

http://docs.unity3d.com/Documentation/ScriptReference/CharacterController.SimpleMove.html

--

I have searched SO for duplicates but the only one i found was this one:

Javascript: what's colon operator in variable name?

and the guy was laughed out of the site

回答1:

Unity's JavaScript isn't actually JavaScript but a heavily modified variant of ECMAScript. It's also called UnityScript, which is a more accurate name.

The colon in variable declaration is used in Unity for specifying the type of the variable. In this case controller is of type CharacterController which in turn is a class. The syntax is invalid in "actual" JavaScript.