JavaScript 对象介绍

在 JavaScript 中,大多数东西都是对象,从像数组这样的核心 JavaScript 功能到构建在 JavaScript 之上的浏览器 APIs。你甚至可以创建自己的对象,将相关函数和变量封装到高效的包中,并充当方便的数据容器。如果你想进一步了解 JavaScript,了解 JavaScript 基于对象的本质非常重要,因此我们提供了此模块来帮助你。在这里,我们详细教授对象理论和语法,然后看看如何创建自己的对象。

¥In JavaScript, most things are objects, from core JavaScript features like arrays to the browser APIs built on top of JavaScript. You can even create your own objects to encapsulate related functions and variables into efficient packages and act as handy data containers. The object-based nature of JavaScript is important to understand if you want to go further with your knowledge of the language, therefore we've provided this module to help you. Here we teach object theory and syntax in detail, then look at how to create your own objects.

先决条件

¥Prerequisites

在开始本模块之前,你应该对 HTMLCSS 有一定的了解。建议你在开始使用 JavaScript 之前先完成 HTML 简介CSS 简介 模块。

¥Before starting this module, you should have some familiarity with HTML and CSS. You are advised to work through the Introduction to HTML and Introduction to CSS modules before starting on JavaScript.

在详细了解 JavaScript 对象之前,你还应该熟悉 JavaScript 基础知识。在尝试本模块之前,请先完成 JavaScript 第一步JavaScript 构建块

¥You should also have some familiarity with JavaScript basics before looking at JavaScript objects in detail. Before attempting this module, work through JavaScript first steps and JavaScript building blocks.

注意:如果你正在使用无法创建自己的文件的计算机/平板电脑/其他设备,你可以尝试在线编码程序(例如 JSBinGlitch)中的(大部分)代码示例。

¥Note: If you are working on a computer/tablet/other devices where you are not able to create your own files, you could try out (most of) the code examples in an online coding program such as JSBin or Glitch.

指南

¥Guides

对象基础知识

在第一篇介绍 JavaScript 对象的文章中,我们将介绍基本的 JavaScript 对象语法,并重新审视我们在课程前面已经介绍过的一些 JavaScript 功能,重申你已经处理过的许多功能这一事实 with 实际上是对象。

对象原型

原型是 JavaScript 对象相互继承特性的一种机制,它们的工作方式与经典面向对象编程语言中的继承机制不同。在本文中,我们将探讨原型链的工作原理。

面向对象编程

在这篇文章中,我们将描述 "classical" 面向对象编程的一些基本原理,并看看它与 JavaScript 中的原型模型的不同之处。

JavaScript 中的类

JavaScript 为想要实现 "classical" 面向对象程序的人们提供了一些功能,在本文中,我们将描述这些功能。

处理 JSON 数据

JavaScript 对象表示法(JSON)是一种基于文本的标准格式,用于表示基于 JavaScript 对象语法的结构化数据,通常用于在 Web 上表示和传输数据(即从服务器向客户端发送一些数据,因此 它可以显示在网页上)。你会经常遇到它,因此在本文中,我们为你提供使用 JavaScript 处理 JSON 所需的所有内容,包括解析 JSON 以便你可以访问其中的数据项,以及编写你自己的 JSON。

对象构建实践

在之前的文章中,我们研究了所有基本的 JavaScript 对象理论和语法细节,为你提供了坚实的基础。在本文中,我们将深入实践练习,为你提供更多构建自定义 JavaScript 对象的练习,这些对象会产生一些有趣且丰富多彩的东西 - 一些彩色的弹跳球。

评估

¥Assessments

为我们的弹跳球演示添加功能

在此评估中,你应该使用上一篇文章中的弹跳球演示作为起点,并向其中添加一些新的有趣的功能。

也可以看看

¥See also

学习 JavaScript

对于有抱负的 Web 开发者来说是一个极好的资源 — 在交互式环境中学习 JavaScript,通过简短的课程和交互式测试,并以自动评估为指导。前 40 节课程是免费的,完整的课程只需一次性支付少量费用。