colon in variable declaration

瘦欲@ 提交于 2019-12-10 10:43:08

问题


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.



来源:https://stackoverflow.com/questions/18940578/colon-in-variable-declaration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!