JavaScript 和 Java 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/245062/
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-08-11 11:58:26  来源:igfitidea点击:

What's the difference between JavaScript and Java?

javajavascript

提问by Guy

What's the difference between JavaScript and Java?

JavaScript 和 Java 有什么区别?

采纳答案by Greg Hewgill

Java and Javascript are similar like Car and Carpet are similar.

Java 和 Javascript 很相似,就像 Car 和 Carpet 很相似。

回答by Kirk Strauser

Everything. They're unrelated languages.

一切。它们是不相关的语言。

回答by ddaa

Everything.

一切。

JavaScript was named this way by Netscape to confuse the unwary into thinking it had something to do with Java, the buzzword of the day, and it succeeded.

Netscape 以这种方式命名 JavaScript,目的是让粗心的人误以为它与 Java(当时的流行语)有关,并且它成功了。

The two languages are entirely distinct.

这两种语言完全不同。

回答by Chris Jester-Young

Here are some differences between the two languages:

以下是两种语言之间的一些差异:

  • Java is a statically typed language; JavaScript is dynamic.
  • Java is class-based; JavaScript is prototype-based.
  • Java constructors are special functions that can only be called at object creation; JavaScript "constructors" are just standard functions.
  • Java requires all non-block statements to end with a semicolon; JavaScript inserts semicolons at the ends of certain lines.
  • Java uses block-based scoping; JavaScript uses function-based scoping.
  • Java has an implicit thisscope for non-static methods, and implicit class scope; JavaScript has implicit global scope.
  • Java 是一种静态类型语言;JavaScript 是动态的。
  • Java 是基于类的;JavaScript 是基于原型的。
  • Java 构造函数是只能在对象创建时调用的特殊函数;JavaScript“构造函数”只是标准函数。
  • Java 要求所有非块语句以分号结尾;JavaScript 在某些行的末尾插入分号。
  • Java 使用基于块的作用域;JavaScript 使用基于函数的作用域。
  • Javathis对非静态方法有一个隐式作用域,还有一个隐式类作用域;JavaScript 具有隐式全局作用域。

Here are some features that I think are particular strengths of JavaScript:

以下是我认为是 JavaScript 的特殊优势的一些特性:

  • JavaScript supports closures; Java can simulate sort-of "closures" using anonymous classes. (Real closures may be supported in a future version of Java.)
  • All JavaScript functions are variadic; Java functions are only variadic if explicitly marked.
  • JavaScript prototypes can be redefined at runtime, and has immediate effect for all referring objects. Java classes cannot be redefined in a way that affects any existing object instances.
  • JavaScript allows methods in an object to be redefined independently of its prototype (think eigenclasses in Ruby, but on steroids); methods in a Java object are tied to its class, and cannot be redefined at runtime.
  • JavaScript 支持闭包;Java 可以使用匿名类模拟某种“闭包”。(Java 的未来版本可能会支持真正的闭包。)
  • 所有 JavaScript 函数都是可变参数的;Java 函数只有在显式标记时才是可变参数。
  • JavaScript 原型可以在运行时重新定义,并对所有引用对象立即生效。Java 类不能以影响任何现有对象实例的方式重新定义。
  • JavaScript 允许独立于其原型重新定义对象中的方法(想想 Ruby 中的特征类,但在类固醇上);Java 对象中的方法与其类相关联,并且不能在运行时重新定义。

回答by Shog9

One is essentially a toy, designed for writing small pieces of code, and traditionally used and abused by inexperienced programmers.

一个本质上是一个玩具,专为编写小段代码而设计,传统上被没有经验的程序员使用和滥用。

The other is a scripting language for web browsers.

另一种是用于 Web 浏览器的脚本语言。

回答by Don Wakefield

They are independent languages with unrelated lineages. Brendan Eich created Javascript originally at Netscape. It was initially called Mocha. The choice of Javascript as a name was a nod, if you will, to the then ascendant Java programming language, developed at Sun by Patrick Naughton, James Gosling, et. al.

它们是具有无关血统的独立语言。Brendan Eich 最初在 Netscape 中创建了 Javascript。它最初被称为摩卡。如果您愿意,选择 Javascript 作为名称是对当时兴起的 Java 编程语言的一种认可,该语言由 Patrick Naughton、James Gosling 等人在 Sun 开发。阿尔。

回答by Bill the Lizard

JavaScript is an object-oriented scriptinglanguage that allows you to create dynamic HTML pages, allowing you to process input data and maintain data, usually within the browser.

JavaScript 是一种面向对象的脚本语言,允许您创建动态 HTML 页面,允许您处理输入数据和维护数据,通常在浏览器中。

Java is a programming language, core set of libraries, and virtual machine platform that allows you to create compiled programs that run on nearly every platform, without distribution of source code in its raw form or recompilation.

Java 是一种编程语言、核心库集和虚拟机平台,它允许您创建可在几乎所有平台上运行的已编译程序,而无需分发原始形式的源代码或重新编译。

While the two have similar names, they are really two completely different programming languages/models/platforms, and are used to solve completely different sets of problems.

虽然两者名称相似,但它们实际上是两种完全不同的编程语言/模型/平台,用于解决完全不同的问题集。

Also, this is directly from the Wikipedia Javascript article:

此外,这直接来自维基百科Javascript 文章

A common misconception is that JavaScript is similar or closely related to Java; this is not so. Both have a C-like syntax, are object-oriented, are typically sandboxed and are widely used in client-side Web applications, but the similarities end there. Java has static typing; JavaScript's typing is dynamic (meaning a variable can hold an object of any type and cannot be restricted). Java is loaded from compiled bytecode; JavaScript is loaded as human-readable code. C is their last common ancestor language.

一个常见的误解是 JavaScript 与 Java 相似或密切相关。事实并非如此。两者都具有类似 C 的语法,面向对象,通常是沙箱化的,并且广泛用于客户端 Web 应用程序,但相似之处仅此而已。Java 有静态类型;JavaScript 的类型是动态的(意味着一个变量可以保存任何类型的对象并且不能被限制)。Java 从编译后的字节码中加载;JavaScript 被加载为人类可读的代码。C 是他们最后一个共同的祖先语言。

回答by toolkit

Take a look at the Wikipedia link

看看维基百科链接

JavaScript, despite the name, is essentially unrelated to the Java programming language, although both have the common C syntax, and JavaScript copies many Java names and naming conventions. The language was originally named "LiveScript" but was renamed in a co-marketing deal between Netscape and Sun, in exchange for Netscape bundling Sun's Java runtime with their then-dominant browser. The key design principles within JavaScript are inherited from the Self and Scheme programming languages.

尽管名称如此,JavaScript 本质上与 Java 编程语言无关,尽管两者都具有通用的 C 语法,并且 JavaScript 复制了许多 Java 名称和命名约定。该语言最初被命名为“LiveScript”,但在 Netscape 和 Sun 之间的联合营销协议中重新命名,以换取 Netscape 将 Sun 的 Java 运行时与他们当时占主导地位的浏览器捆绑在一起。JavaScript 中的关键设计原则继承自 Self 和 Scheme 编程语言。

回答by Claudiu

They have nothing to do with each other.

他们彼此没有任何关系。

Java is statically typed, compiles, runs on its own VM.

Java 是静态类型的,可以在自己的 VM 上进行编译和运行。

Javascript is dynamically typed, interpreted, and runs in a browser. It also has first-class functions and anonymous functions, which Java does not. It has direct access to web-page elements, which makes it useful for doing client-side processing.

Javascript 是动态输入、解释并在浏览器中运行的。它还具有 Java 所没有的一流函数和匿名函数。它可以直接访问网页元素,这对于进行客户端处理非常有用。

They are also somewhat similar in syntax, but that's about it.

它们在语法上也有些相似,但仅此而已。

回答by Darcy Casselman

Like everybody's saying, they're pretty much entirely different.

就像大家说的,它们几乎完全不同。

However, if you need a scriptinglanguage for your Javaapplication, Javascript is actually a really good choice. There are ways to get Javascript running in the JVM and you can access and manipulate Java classes pretty seamlessly once you do.

但是,如果您的Java应用程序需要一种脚本语言,那么 Javascript 实际上是一个非常好的选择。有很多方法可以让 Javascript 在 JVM 中运行,一旦你这样做了,你就可以非常无缝地访问和操作 Java 类。