Javascript 简明教程

Javascript - Overview

What is JavaScript?

JavaScript 是一种动态计算机编程语言。它是 lightweight ,最常作为网页的一部分来使用,其实现允许客户端脚本与用户交互并创建动态网页。它是一种具有面向对象功能的 interpreted 编程语言。

JavaScript is a dynamic computer programming language. It is lightweight and most commonly used as a part of web pages, whose implementations allow client-side script to interact with the user and make dynamic pages. It is an interpreted programming language with object-oriented capabilities.

JavaScript 是一种 single-threaded 编程语言,我们可以将其用于客户端或服务器端开发。它是一种 dynamically 类型编程语言,这意味着我们在编写 JavaScript 代码时不必在意变量数据类型。此外,它包含有控制语句、运算符,以及 Array、Math、Data 等对象。

JavaScript is a single-threaded programming language that we can use for client-side or server-side development. It is a dynamically typed programming language, which means that we don’t care about variable data types while writing the JavaScript code. Also, it contains the control statements, operators, and objects like Array, Math, Data, etc.

JavaScript 最初被称为 LiveScript ,但 Netscape 将其名称更改为 JavaScript,可能是由于 Java 所产生的兴奋。JavaScript 最初出现在 1995 年的 Netscape 2.0 中,名称为 LiveScript 。该语言的通用核心已被嵌入到 Netscape 和其他网络浏览器中。

JavaScript was first known as LiveScript, but Netscape changed its name to JavaScript, possibly because of the excitement being generated by Java. JavaScript made its first appearance in Netscape 2.0 in 1995 with the name LiveScript. The general-purpose core of the language has been embedded in Netscape and other web browsers.

ECMA-262 Specification 定义了核心 JavaScript 语言的标准版本。

The ECMA-262 Specification defined a standard version of the core JavaScript language.

  1. JavaScript is a lightweight, interpreted programming language.

  2. Designed for creating network-centric applications.

  3. Complementary to and integrated with Java.

  4. Complementary to and integrated with HTML.

  5. Open and cross-platform

History of JavaScript

JavaScript 是由网景通信公司的计算机科学家和程序员 Brendan Eich 开发的。JavaScript 最开始的名字是“Mocha”。之后,它改名为“LiveScript”,然后改名为“JavaScript”。

JavaScript is developed by Brendan Eich, a computer scientist and programmer at Netscape Communications Corporation. The initial name of the JavaScript was the 'Mocha'. After that, it changed to 'LiveScript', and then 'JavaScript'.

在 1996 年至 1997 年期间,欧洲计算机制造商协会 (ECMA) 对 JavaScript 进行了标准化。之后,对 JavaScript 进行了 3 次修订。

Between 1996 and 1997, the European Computer Manufacturers Association (ECMA) standardized JavaScript. After that, 3 revisions of the JavaScript have been done.

ES5 (2009 年),推出了 Node.js,以便将 JavaScript 作为服务器端语言进行使用。 ES6 (2015 年)对 JavaScript 进行了重大修订,在 JavaScript 中引入了高级特性。

In ES5 (2009), Node.js was introduced to use JavaScript as a server-side language. The ES6 (2015) was a significant revision of JavaScript, introducing advanced features into JavaScript.

当前,JavaScript 已达到 ES14 版本。ES14(ECMAScript 2023)是第 14 个版本,于 2023 年 6 月发布。

Currently, JavaScript has reached the version ES14. ES14 (ECMAScript 2023) the 14th version, was released in June 2023.

Client-Side JavaScript

客户端 JavaScript 是该语言最常用的形式。脚本应包含在 HTML 文档中或由其引用,这样浏览器才能解释该代码。

Client-side JavaScript is the most common form of the language. The script should be included in or referenced by an HTML document for the code to be interpreted by the browser.

这意味着网页不必是静态 HTML,而是可以包含与用户交互、控制浏览器,并动态创建 HTML 内容的程序。

It means that a web page need not be a static HTML but can include programs that interact with the user, control the browser, and dynamically create HTML content.

JavaScript client-side 机制相较于传统的 CGI 服务器端脚本提供了诸多优势。例如,你可能使用 JavaScript 来检查用户是否在表单字段中输入了有效的电子邮件地址。

The JavaScript client-side mechanism provides many advantages over traditional CGI server-side scripts. For example, you might use JavaScript to check if the user has entered a valid e-mail address in a form field.

JavaScript 代码会在用户提交表单时执行,并且只会将所有条目都有效时才会将其提交到 Web 服务器。

The JavaScript code is executed when the user submits the form, and only if all the entries are valid they would be submitted to the Web Server.

JavaScript 可用于捕获用户发起的事件,例如按钮点击、链接导航以及用户显式或隐式发起的其他操作。

JavaScript can be used to trap user-initiated events such as button clicks, link navigation, and other actions that the user initiates explicitly or implicitly.

用于 JavaScript 开发的流行客户端端库是 ReactJS、NextJS、Vue JS、Angular JS 等。

The Popular client-side libraries for JavaScript development are ReactJS, NextJS, Vue JS, Angular JS, etc.

Server-Side JavaScript

早期,JavaScript 被用于前端开发,以向 HTML 页面添加行为。自 2009 年起,JavaScript 也被用作一门服务端编程语言,用于构建可扩展且动态的 Web 应用程序。

In the early days, JavaScript was used for front-end development to add behaviors to HTML pages. Since 2009, JavaScript is also used as a server-side programming language to build scalable and dynamic web applications.

Node.js 是使用 JavaScript 构建应用程序服务器的最佳且最流行的 JavaScript 运行时环境之一。利用 Node.js ,我们可以在浏览器外执行 JavaScript 代码并管理服务端任务。这些服务端任务可能是与数据库、API、文件处理或网络通信的交互。由于 Node.js 的事件驱动架构,它比其他服务端编程语言更快。

Node.js is one of the best and most popular JavaScript runtime environments for building the server of applications using JavaScript. Using Node.js, we can execute the JavaScript code outside the browser and manage the server task. The server tasks can be an interaction with the database, APIS, file handling, or maybe network communication. Due to the event-driven architecture of Node.js, it is faster than other server-side programming languages.

Advantages of JavaScript

使用 JavaScript 的优点为 −

The merits of using JavaScript are −

  1. Less server interaction − You can validate user input before sending the page off to the server. This saves server traffic, which means less load on your server.

  2. Immediate feedback to the visitors − They don’t have to wait for a page reload to see if they have forgotten to enter something.

  3. Increased interactivity − You can create interfaces that react when the user hovers over them with a mouse or activates them via the keyboard.

  4. Richer interfaces − You can use JavaScript to include such items as drag-and-drop components and sliders to give a Rich Interface to your site visitors.

Limitations of JavaScript

我们不能将 JavaScript 当作一门成熟的编程语言。它缺少以下重要特性 −

We cannot treat JavaScript as a full-fledged programming language. It lacks the following important features −

  1. Client-side JavaScript does not allow the reading or writing of files. This has been kept for security reasons.

  2. JavaScript cannot be used for networking applications because no such support is available.

  3. JavaScript doesn’t have any multi-threading capabilities.

Imperative vs. Declarative JavaScript

命令式和声明式是一种编程范式,JavaScript 同时遵循这两个范式。

The imperative and declarative is a programming paradigm, and JavaScript follows both.

  1. Imperative JavaScript − In imperative JavaScript, we write code in the manner that the code describes the steps to get the output. So, we are concerned about the code execution flow and output both. For example, to sum all array elements, if we write code for loop, it explains each step to get the sum.

  2. Declarative JavaScript − In declarative JavaScript, we don’t need to worry about execution flow, but we should get the correct output at the end. For example, we use a built-in array.reduce() method to get a sum of array elements. Here, we don’t concern about how reduce() method is implemented in the library.

JavaScript Development Tools

JavaScript 的主要优势之一是它不需要昂贵的开发工具。你可以从一个简单的文本编辑器(例如记事本)开始。由于它是在 Web 浏览器上下文中解释执行的语言,你甚至不需要购买编译器。

One of the major strengths of JavaScript is that it does not require expensive development tools. You can start with a simple text editor such as Notepad. Since it is an interpreted language inside the context of a web browser, you don’t even need to buy a compiler.

以下是一些免费工具,它们在使用 JavaScript 开发应用程序时非常有用。

Here are various free tools which can be helpful while developing applications with JavaScript.

  1. Visual Studio Code (VS Code) − The VS Code is a code editor used by most developers to write JavaScript code. It is feature rich and contains various extensions that can increase the productivity of any developer.

  2. Chrome dev tools − Programmers may use the Chrome dev tools to debug the JavaScript code. However, they can use the debugging tool of any browser as most browser comes with it.

以上两个工具可以提高开发人员编写代码的工作效率。此外,你可以使用其他工具,例如用于版本控制的 Git、用于构建应用程序的 Webpack 等。

The above 2 tools increase the productivity of the developer for writing the code. Furthermore, you may use other tools like Git for version controlling, Webpack to build your application, etc.

Where is JavaScript Today?

2015 年,推出了 ES6 版本的 JavaScript,其中有重大的增强,包括面向对象的概念、匿名函数、模板字面量等。在 June 2023 中,推出了 JavaScript 的第 14 个版本 ES14(ECMAScript 2023)。

In 2015, the ES6 version of JavaScript was launched with significant enhancements, including object-oriented concepts, anonymous functions, template literals, etc. In June 2023, the ES14 (ECMAScript 2023), the 14th version of JavaScript was launched.