还有哪些其他语言像 JavaScript 一样是松散类型的?

发布于 2025-01-03 19:16:41 字数 61 浏览 1 评论 0原文

我了解到 JavaScript 是一种“松散类型”语言。

还有哪些其他语言是“松散类型”的?

I learned that JavaScript is a "loosely-typed" language.

What other languages are "loosely-typed?"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

海螺姑娘 2025-01-10 19:16:41

不同意 关于“松散”或“弱类型”的确切含义,但是,就通常理解而言,“松散类型”指的是一种更宽容的语言类型规则,甚至可能将类型从一种类型隐式转换为另一种类型

。 ”维基百科:

Liskov 和 Zilles 将强类型语言定义为“每当一个对象从调用函数传递到被调用函数时,其类型必须与被调用函数中声明的类型兼容。”

根据这个定义,JavaScript 是松散类型的(即与强类型相反),因为大多数 JavaScript 运算符会在必要时强制其操作数。

例如:

[2]-1      // 1 (equivalent to 2-1)
[2]+1      // 21 (equivalent to String([2]) + '1')
true && {} // No error, returns `{}`

另一个松散类型语言示例是 C

请注意,JavaScript 也是动态类型的。这意味着值的类型绑定到值并在运行时检查,而不是绑定到变量并在编译时检查。实际上,类型检查是“动态”(即在运行时)执行的,而不是“静态”(即在编译时)执行的。

动态语言示例:

  • ActionScript
  • Clojure
  • Lisp
  • Lua
  • Perl
  • Python
  • Ruby
  • Smalltalk

There is disagreement about exactly what "loose" or "weak typing means, however, in so far as commonly understood, "loose typing" refers to a language that has more forgiving typing rules, and might even implicitly convert types from one type to another.

From Wikipedia:

Liskov and Zilles defined a strongly-typed language as one in which "whenever an object is passed from a calling function to a called function, its type must be compatible with the type declared in the called function."

According to this definition, JavaScript is loosely-typed (ie. the opposite of strongly typed) because most of JavaScript operators will coerce their operands if necessary.

For example:

[2]-1      // 1 (equivalent to 2-1)
[2]+1      // 21 (equivalent to String([2]) + '1')
true && {} // No error, returns `{}`

Another example of a loosely-typed language example is C.

Note that JavaScript is also dynamically typed. This means that the type of a value is bound to the value and checked at runtime, instead of being bound to the variable and checked at compile time. In effect type checking is performed "dynamically" (ie at runtime) as opposed to "statically" (ie. at compile time).

Examples of dynamic languages:

  • ActionScript
  • Clojure
  • Lisp
  • Lua
  • Perl
  • Python
  • Ruby
  • Smalltalk
情深已缘浅 2025-01-10 19:16:41

PHP 与PERL 也是弱类型的。

PHP & PERL are also weakly typed.

昇り龍 2025-01-10 19:16:41

许多解释性语言如php或pearl,总之松散类型语言是一种不需要定义变量的语言

many of the interpreted languages such as php or pearl, in short a loosely-typed language is a language that does not require a variable to be defined

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文