这篇翻译不完整。请帮忙从英语翻译这篇文章

本章节介绍了 JavaScript ,并且讨论了一些基本概念。

你应该知道哪些知识?

该指南假定你拥有如下基础背景:

  • 对互联网和万维网 (WWW) 有基本的认识。
  • 熟悉超文本标记语言(HTML)。
  • 一些编程经验。如果你初识编程,试试主页列出的有关 JavaScript 的教程。

去哪里发现JavaScript的信息

MDN上的JavaScript文档包括以下内容:

  • 认识Web  为初学者提供信息和介绍一些关于编程和互联网的基本概念。
  • JavaScript入门 (本指导) 提供关于JavaScrip这门语言和它的目标的概述。
  • JavaScript 参考  提供详细的关于JavaScript的参考资料.

如果你刚开始学习JavaScript,请详细阅读 learning areaJavaScript Guide.的文章。假如你对JavaScript基础有很深的了解, 你可以使用 JavaScript Reference 来获取更多关于私人对象和语句的细节。

什么是JavaScript?

JavaScript 是一门跨平台、面向对象的轻量级脚本语言。 在主机环境中, JavaScript能够通过连接环境对象而实现可编译控制.

JavaScript内置了一个对象的标准库,比如数组,日期,数学和一个语言元素核心集合包括操作符,流程控制符以及语句。JavaScript核心部分可以通过组合已有语言核心对象来扩展语言以适应不同用途;例如:

  • 客户端的JavaScript通过提供控制浏览器及其文档对象模型(DOM)的对象来扩展语言核心。例如:客户端版本直接支持应用将元素放在在HTML表单中并且支持响应用户事件比如鼠标点击、表单提交和页面导航。
  •  服务端的JavaScript则通过提供有关在服务器上运行JavaScript的对象来可扩展语言核心。例如:服务端版本直接支持应用和数据库通信,提供应用不同调用间的信息连续性,或者在服务器上执行文件操作。

JavaScript和Java

JavaScript和Java有一些共性但是在另一些方面有着根本性区别。JavaScript酷似Java但是并没有Java的静态类型和强类型检查特性。JavaScript遵循了Java的表达式语法,命名规范以及基础流程控制,这也是JavaScript从LiveScript更名的原因。

比Java的通过声明构建类的编译时系统以及JavaScript支持的基于少量的数据类型比如数字、布尔、字符串值的运行时系统。JavaScript拥有基于原型的对象模型提供的动态继承;也就是说,也就是说,独立对象的继承是可以改变的。 JavaScript 支持匿名函数。 函数也可以作为对象的属性执行。

与Java相比,Javascript是一门形式自由的语言。你不必声明所有的变量,类和方法。你不必关心方法是否是 共有、私有或者受保护的,也不需要实现接口。无需显式指定变量、参数、方法返回值的数据类型。

Java是基于类的编程语言,设计的初衷就是为了快速执行和类型安全的。类型安全,举例来说,你不能将一个Java 整数变量 转化为一个对象引用,或者由Java字节码访问专有存储器。Java基于类的模型,意味着程序包含专有的类及其方法。Java的类继承和强类型要求紧耦合的对象层级结构。这些要求使得Java编程比JavaScript要复杂的多。

相比之下,JavaScript传承了HyperTalk和dBASE语句精简、动态类型等精髓,为更多开发者提供了一种语法简单、内置功能强大以及用最小需求创建对象的编程工具。

JavaScript 和 Java 的对比
JavaScript Java
面向对象。不区分对象类型。通过原型机制继承,任何对象的属性和方法均可以被动态添加。 基于类系统。分为类和实例,通过类层级的定义实现继承。不能动态增加对象或类的属性或方法。
变量类型不需要提前声明(动态类型)。 变量类型必须提前声明(静态类型)。
不能直接自动写入硬盘。 可以直接自动写入硬盘。

请查看章节对象模型的详情来了解更多JavaScript和Java的不同。

JavaScript and the ECMAScript specification

JavaScript is standardized at Ecma International — the European association for standardizing information and communication systems (ECMA was formerly an acronym for the European Computer Manufacturers Association) to deliver a standardized, international programming language based on JavaScript. This standardized version of JavaScript, called ECMAScript, behaves the same way in all applications that support the standard. Companies can use the open standard language to develop their implementation of JavaScript. The ECMAScript standard is documented in the ECMA-262 specification. See New in JavaScript to learn more about different versions of JavaScript and ECMAScript specification editions.

The ECMA-262 standard is also approved by the ISO (International Organization for Standardization) as ISO-16262. You can also find the specification on the Ecma International website. The ECMAScript specification does not describe the Document Object Model (DOM), which is standardized by the World Wide Web Consortium (W3C). The DOM defines the way in which HTML document objects are exposed to your script. To get a better idea about the different technologies that are used when programming with JavaScript, consult the article JavaScript technologies overview.

JavaScript documentation versus the ECMAScript specification

The ECMAScript specification is a set of requirements for implementing ECMAScript; it is useful if you want to implement standards-compliant language features in your ECMAScript implementation or engine (such as SpiderMonkey in Firefox, or v8 in Chrome).

The ECMAScript document is not intended to help script programmers; use the JavaScript documentation for information on writing scripts.

The ECMAScript specification uses terminology and syntax that may be unfamiliar to a JavaScript programmer. Although the description of the language may differ in ECMAScript, the language itself remains the same. JavaScript supports all functionality outlined in the ECMAScript specification.

The JavaScript documentation describes aspects of the language that are appropriate for a JavaScript programmer.

JavaScript上手

Javascript的上手是非常简单的,你仅仅只需要一个现代化的浏览器就可以. 这篇教程包含了一些只在最新版本的firefox上才能正常显示的特性,所以建议大家使用最新版本的浏览器

Firefox内置了两款非常棒的工具用来实验JavaScript:浏览器控制台和Scratchpad。

浏览器控制台

The Web Console shows you information about the currently loaded Web page, and also includes a command line that you can use to execute JavaScript expressions in the current page.

To open the Web Console, select "Web Console" from the "Web Developer" menu, which is under the "Tools" menu in Firefox. It appears at the bottom of the browser window. Along the bottom of the console is a command line that you can use to enter JavaScript, and the output appears in the pane above:

Scratchpad

The Web Console is great for executing single lines of JavaScript, but although you can execute multiple lines, it's not very convenient for that, and you can't save your code samples using the Web Console. So for more complex examples Scratchpad is a better tool.

To open Scratchpad, select "Scratchpad" from the "Web Developer" menu, which is under the "Tools" menu in Firefox. It opens in a separate window and is an editor that you can use to write and execute JavaScript in the browser. You can also save scripts to disk and load them from disk.

If you choose "Inspect", the code in your pad is executed in the browser and the result is inserted back into the pad as a comment:

Hello world

学习JavaScript的第一步,打开浏览器的代码调试器尝试写你的第一个JavaScript版本的“Hello world”程序。

function greetMe(user) {
  return "Hi " + user;
}

greetMe("Alice"); // "Hi Alice"

在Scratchpad(代码草稿纸)中选择要执行的代码,然后点击快捷键Ctrl+R就可以在浏览器中看到选中代码的执行结果。

在接下来的章节里,该指南将介绍JavaScript的语法以及语言特性,届时你将可以使用它编写更加复杂的程序。

文档标签和贡献者

最后编辑者: MurphyL,
隐藏侧边栏