Javascript 简明教程

Javascript - Features

JavaScript Features

JavaScript 是一种非常流行且广泛用于 Web 开发的编程语言。它具有多种特性,使其强大且灵活。其中一些特性包括动态、轻量级、解释型、函数式和面向对象。

JavaScript is a highly popular and widely-used programming language for web development. It has a variety of features that make it powerful and flexible. Some of these features include being dynamic, lightweight, interpreted, functional, and object-oriented.

有很多开源 JavaScript 库,这有助于促进在前端和后端开发中利用 JavaScript。让我们重点介绍 JavaScript 的一些主要特性。

Many open-source JavaScript libraries are available, facilitating the utilization of JavaScript in both frontend as well as backend development. Let’s highlight some of the key features of JavaScript.

Easy Setup

我们不需要特定的编辑器来开始编写 JavaScript 代码。甚至任何人都可以在记事本中编写 JavaScript 代码。此外,JavaScript 可以在浏览器中执行,而无需任何解释器或编译器设置。

We don’t need a particular editor to start writing the JavaScript code. Even anyone can write JavaScript code in NotePad. Also, JavaScript can be executed in the browser without any interpreter or compiler setup.

你可以使用 <script > 标签在 HTML 文件中添加 JavaScript。但是,它还允许你从外部 JavaScript 文件(具有 ' .js ' 扩展名)将 JavaScript 添加到网页。

You can use the <script > tag to add JavaScript in the HTML file. However, it also allows you to add JavaScript to the web page from the external JavaScript file, having '.js' extension.

Browser Support

所有浏览器都支持 JavaScript,因为所有现代浏览器都带有内置的 JavaScript 执行环境。

All browsers support JavaScript, as all modern browser comes with the built-in JavaScript execution environment.

但是,你也可以使用“window”对象来检查浏览器是否支持 JavaScript 或其特定特性。

However, you can also use the ‘window’ object to check whether the browser supports JavaScript or its particular feature.

Dom Manipulation

JavaScript 允许开发者操作网页元素。此外,你还可以控制浏览器。

JavaScript allows developers to manipulate the webpage elements. Also, you can control the browser.

它包含各种方法来使用不同的属性访问 DOM 元素,并允许自定义 HTML 元素。

It contains the various methods to access the DOM elements using different attributes and allows to customize the HTML elements.

Event Handling

JavaScript 允许你处理用于与网页交互的 events

JavaScript allows you to handle the events used to interact with the web page.

例如,你可以使用 JavaScript 检测对特定 HTML 元素的鼠标点击,并与 HTML 元素进行交互。

For example, you can detect the mouse click on a particular HTML element using JavaScript and interact with the HTML element.

还有一些其他事件,例如检测网页的滚动行为等。我们将在“JavaScript 事件”章节中探讨所有事件。

Some other events also exist, like detecting the scrolling behavior of web page, etc. We will explore all events in the ‘JavaScript events’ chapter.

Dynamic Typing

JavaScript 在运行时决定变量的类型。因此,在编写代码的时候我们不需要关心变量的数据类型,这让编写代码更灵活。

JavaScript decides the type of variables at runtime. So, we don’t need to care about variable data type while writing the code, providing more flexibility to write code.

另外,你可以给一个变量分配不同数据类型的值。例如,如果你已经存储了一个特定变量的数值,你可以用字符串来更新变量的值。

Also, you can assign the values of the different data types to a single variable. For example, if you have stored the number value of a particular variable, you can update the variable’s value with the string.

Functional Programming

JavaScript 支持 functional 编程。在 JavaScript 中,你可以定义一等函数、纯函数、闭包、高阶函数、箭头函数、函数表达式等。

JavaScript supports the functional programming. In JavaScript, you can define the first-class function, pure functions, closures, higher-order functions, arrow funcitons, function expresions, etc.

它通常把函数用作解决问题的基本构建块。

It mostly uses the functions as a primary building blocks to solve the problem.

Cross-platform Support

每个操作系统和浏览器都支持 JavaScript。因此,它被广泛用于开发网站、移动应用程序、游戏、桌面应用程序等。

Each operating system and browser support JavaScript. So, it is widely used for developing websites, mobile applications, games, desktop applications, etc.

Object-oriented Programming

JavaScript 包含类,我们可以使用它的功能实现所有 object-oriented 编程概念。

JavaScript contains the classes, and we can implement all object-oriented programming concepts using its functionality.

它还支持面对对象编程的概念,如继承、抽象、多态、封装等。

It also supports inheritance, abstraction, polymorphism, encapsulation, etc, concepts of Object-oriented programming.

Built-in Objects

JavaScript 包含了内置的对象,如 Math 和 Date。我们可以使用 Math 对象来执行数学运算,使用 Date 对象来轻松地处理日期。

JavaScript contains built-in objects like Math and Date. We can use a Math object to perform mathematical operations and a Date object to manipulate the date easily.

不过,你也可以操作内置对象的功能。

However, you can also manipulate the functionality of the built-in object.

Object Prototypes

在 JavaScript 中,一切皆为 object 。例如,数组、函数、数字、字符串、布尔值、集合、映射等都是对象。

In JavaScript, everything is an object. For example, array, function, number, string, boolean, set, map, etc. are objects.

每个对象都包含一个隐藏的原型属性。你可以使用原型属性来实现继承或扩展类或对象的功能,借助于其他对象的特性。

Each object contains the prototype property, which is hidden. You can use the prototype property to achive inheritance or extend the functionality of class or object, by other object’s functionality.

Global Object

JavaScript 包含 global object 来访问可供任何地方使用的变量。

JavaScript contains the global object to access the variables which are available everywhere.

若要在浏览器中访问全局变量,你可以使用 window 对象,在 Node.js 中,你可以使用“global”关键字来访问全局变量。

To access global variables in the browser, you can use the window object, and in Node.js, you can use the 'global' keyword to access global variables.

最近引入了 globalThis 关键字来访问全局变量,大多数运行时环境都支持此关键字。

Recently, globalThis keyword is introduced to access the global variables, and which is supported by the most runtime environments.

Built-in Methods

JavaScript 还包含对每个对象而设的内置方法。开发者可以使用内置方法来编写高效且更短的代码。

JavaScript also contains the built-in methods for each object. Developers can use the built-in methods to write efficient and shorter codes.

例如,Array 对象包含 filter() 方法来过滤数组元素, sort() 方法来对数组排序。String 对象包含 replace() 方法来替换字符串中的文本, trim() 方法来移除字符串中的空格等。

For example, the Array object contains the filter() method to filter array elements and the sort() method to sort the array. The String object contains the replace() method to replace text in the string, the trim() method to remove whitespaces from the string, etc.

Modular Programming

JavaScript 允许你在不同的模块中编写代码,并把它们与父模块连接起来。因此,开发者可以编写可维护的代码。

JavaScript allows you to write the code in different modules and connect them with the parent module. So developers can write maintainable code.

通过在单独的模块中编写代码,你可以减少代码的复杂性并在需要时重复使用每个模块。

By writing the code in a separate module, you can reduce the complexity of the code and reuse each module whenever you require.

JSON

JSON 代表 JavaScript object notation 。这是一种广泛使用的数据格式,用于在两个网络之间交换数据。例如,服务器和客户端。

JSON stands for JavaScript object notation. It is a widely used data format to exchange data between two networks. For example, server and client.

JavaScript 也支持 JSON 格式来存储数据。

JavaScript also supports the JSON format to store the data.

Asynchronous Programming

JavaScript 是 single-threaded 编程语言。要更快地执行代码,可以使用 asynchronous 编程。

JavaScript is a single-threaded programming language. To execute your code faster, you can use asynchronous programming.

可以在 JavaScript 中使用 promises 来编写异步代码,从而使我们能够异步地处理多个任务。

You can use promises in JavaScript to write asynchronous code, allowing us to handle multiple tasks asynchronously.

Even-driven Architecture

JavaScript 的事件驱动架构允许开发者创建互动性和响应性的 Web 应用程序,同时处理大量用户群。

The event-driven architecture of JavaScript allows developers to create interactive and responsive web applications by handling a large user base concurrently.

由于 JavaScript 的功能和应用非常广泛,所以 Facebook 的前端就是建立在 JavaScript 之上的。Netflix 是使用 JavaScript 的 ReactJS 框架构建的。类似地,亚马逊、PayPal、Airbnb、领英、推特等也都使用了 JavaScript 来构建。

Due to the vast features and applications of JavaScript, the front end of Facebook is built on JavaScript. Netflix is built using the ReactJS framework of JavaScript. Similarly, Amazon, PayPal, Airbnb, LinkedIn, Twitter, etc., are also built using JavaScript.

Server-side Support

JavaScript 的 Node.js 运行时环境广泛用于创建应用程序的后端,因为 javaScript 也可用于创建服务器。它允许你为应用程序创建一个可扩展的后端。

The Node.js runtime environment of JavaScript is widely used to create the backend of the application, as javaScript can also be used to create servers. It allows you to create a scalable backend for the application.