变量声明中的冒号 [英] colon in variable declaration

查看:126
本文介绍了变量声明中的冒号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读一些Unity教程,并在其中一个示例脚本中遇到了这一行代码.我熟悉javascript,但从未见过在声明变量(对象文字除外)时使用的冒号.

var controller : CharacterController = GetComponent(CharacterController);

此行中的冒号是什么.

在脚本的末尾可能还有另一行相关的代码

@script RequireComponent(CharacterController)

这是来自unity的完整示例代码:

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

-

我已经在SO中搜索重复项,但是我发现的唯一一个就是这个重复项:

JavaScript:变量名中的冒号运算符是什么?

那个家伙被网站嘲笑了

解决方案

Unity的JavaScript实际上不是JavaScript,而是ECMAScript的经过重大修改的变体.它也称为UnityScript,这是一个更准确的名称.

变量声明中的冒号用于 Unity 中,用于指定变量的类型.在这种情况下,controller是类型CharacterController的类型,而该类型又是一个类.该语法在实际" JavaScript中无效.

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

解决方案

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.

这篇关于变量声明中的冒号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆