JavaScript 是“动态的”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32476680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:20:16  来源:igfitidea点击:

What does it mean that JavaScript is "dynamic"?

javascriptdynamicstatic

提问by OpMt

I've read from different sources (e.g. wiki, articles, etc.) what dynamic in a programming sense means. Wikipediatalks about how dynamic programming languages execute certain programming behaviors at runtime (as opposed to compile time for static languages), but their explanation is vague and talks about how these behaviors vary in degree of difficulty, complexity, and performance costs for all programming languages.

我从不同的来源(例如 wiki、文章等)阅读了编程意义上的动态意味着什么。维基百科讨论动态编程语言如何在运行时执行某些编程行为(与静态语言的编译时相反),但他们的解释含糊不清,并讨论了这些行为如何在难度、复杂性和所有编程语言的性能成本方面有所不同.

So with respect to JavaScript specifically, what does it mean that it's dynamic?

那么特别是关于 JavaScript,它是动态的意味着什么?

I may be completely wrong on this, but also understand JavaScript to be a dynamically typed language since you don't have state the type before instantiating a variable/function(e.g. var, function jsFunction()) as opposed to a statically typed language like Java where you define a type before instantiating a variable/function(e.g. int var, public int function()).

我可能完全错了,但我也理解 JavaScript 是一种动态类型语言,因为在实例化变量/函数(例如 var、函数 jsFunction())之前你没有声明类型,而不是像静态类型语言一样在实例化变量/函数之前定义类型的 Java(例如 int var、public int function())。

Does this have to do with any of this?

这与这些有关系吗?

采纳答案by Guffa

Most languages have some aspect of dynamic behaviour. Even statically typed languages can have a dynamic or variant data type that can contain different data types.

大多数语言都有动态行为的某些方面。即使是静态类型的语言也可以具有包含不同数据类型的动态或变体数据类型。

JavaScript is called a dynamic language because it doesn't just have a few dynamic aspects, pretty much everything is dynamic.

JavaScript 之所以被称为动态语言,是因为它不仅具有一些动态方面,而且几乎所有内容都是动态的。

All variables are dynamic (both in type and existance), and even the code is dynamic. You can create new variables at runtime, and the type of variables is determined at runtime. You can create new functions at any time, or replace existing functions. When used in a browser, code is added when more script files are loaded, and you can load more files any time you like.

所有变量都是动态的(无论是类型还是存在),甚至代码也是动态的。您可以在运行时创建新变量,并且在运行时确定变量的类型。您可以随时创建新功能,或替换现有功能。在浏览器中使用时,加载更多脚本文件时添加代码,您可以随时加载更多文件。

Nowadays JavaScript is compiled in many implementations, and static code and static types are generated in the background. However, the behaviour is still dynamic, the compiler only generates static types when it finds that the dynamic aspects are not used for a specific object.

现在很多实现都是编译JavaScript,后台生成静态代码和静态类型。但是,行为仍然是动态的,编译器仅在发现动态方面未用于特定对象时才生成静态类型。

回答by liban

The most meaningful well-defined way in which JS is dynamic is that it's dynamically typed: the language has data types, but does not check that a program's types are "okay" until the program is actually running. The opposite is statically typed, meaning that programs' types are verified by a program that inspects their source code before they are run. (E.g., Java and ML are statically typed.)

JS 动态的最有意义的定义明确的方式是它是动态类型的:语言具有数据类型,但在程序实际运行之前不会检查程序的类型是否“正常”。相反的是静态类型,这意味着程序的类型由在运行之前检查其源代码的程序进行验证。(例如,Java 和 ML 是静态类型的。)