JavaScript 是一种无类型语言吗? [英] Is JavaScript an untyped language?

查看:22
本文介绍了JavaScript 是一种无类型语言吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现有些人称 JavaScript 为动态、弱类型"的语言,但有些人甚至说无类型"?哪个是真的?

I've found that some people call JavaScript a "dynamically, weakly typed" language, but some even say "untyped"? Which is it really?

推荐答案

JavaScript is 无类型:

JavaScript is untyped:


(来源:no.gd)

即使是 Brendan Eich 也这么说.在 Twitter 上,他回复了与此问题相关的主题:

Even Brendan Eich says so. On Twitter, he replied to a thread that linked to this question:

...学术类型使用无类型";表示无静态类型"...

所以问题在于无类型有几种不同的定义.

So the problem is that there's a few different definitions of untyped.

在上述答案之一中讨论了一个定义 - 运行时不标记值,只是将每个值视为位.JavaScript 确实 标记值并根据这些标记具有不同的行为.所以 JavaScript 显然不适合这个类别.

One definition has been talked about in one of the above answers - the runtime doesn't tag values and just treats each value as bits. JavaScript does tag values and has different behaviour based on those tags. So JavaScript obviously doesn't fit this category.

另一个定义来自编程语言理论(Brendan 所指的学术内容).在此域中,untyped 仅表示 一切都属于一种类型.

The other definition is from Programming Language Theory (the academic thing that Brendan is referring to). In this domain, untyped just means everything belongs to a single type.

为什么?因为一种语言只有在可以证明类型对齐时才会生成程序(又名 库里-霍华德对应;类型是定理,程序是证明).这意味着在无类型语言中:

Why? Because a language will only generate a program when it can prove that the types align (a.k.a. the Curry-Howard correspondence; types are theorems, programs are proofs). This means in an untyped language:

  1. 一个程序总是生成
  2. 因此类型总是匹配
  3. 因此必须只有一种类型
  1. A program is always generated
  2. Therefore types always match up
  3. Therefore there must only be one type

与类型化语言相比:

  1. 一个程序可能不会生成
  2. 因为类型可能不匹配
  3. 因为一个程序可以包含多种类型
  1. A program might not be generated
  2. Because types might not match up
  3. Because a program can contain multiple types

所以,在 PLT 中,untyped 仅表示 动态类型typed 仅表示 静态类型.JavaScript 在此类别中绝对是无类型的.

So there you go, in PLT, untyped just means dynamically typed and typed just means statically typed. JavaScript is definitely untyped in this category.

另见:

这篇关于JavaScript 是一种无类型语言吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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