As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center提供指导。




已关闭8年。




标题说明了一切,但我将提供更多说明:

在看到许多将所有变量都声明为var类型的javascript示例并看到了对其他数据类型的支持之后,为什么不将特定数据类型的变量声明为此类呢?意思是,为什么不是这样:
string hello = 'Hello, World'
用于代替
var hello = 'Hello, World'
查看类似于OReilly Javascript的站点,表明存在其他类型的保留字。同样,为什么不使用它们呢?它不会像这样行吗:不再需要typeof(variable)==='string';

最佳答案

很简单,JavaScript变量没有类型。值具有类型。

该语言允许我们编写如下代码:

var foo = 42;
foo = 'the answer';
foo = function () {};

因此,在变量声明中指定类型将毫无意义,因为类型由变量的值决定。这在“动态”语言中相当普遍。

09-10 05:44
查看更多