Skip to Content
JavaScript 经典实例:第三版
book

JavaScript 经典实例:第三版

by Adam D. Scott, Matthew MacDonald, Shelley Powers
September 2022
Intermediate to advanced
585 pages
8h 17m
Chinese
China Electric Power Press Ltd.
Content preview from JavaScript 经典实例:第三版
201
8
JavaScript
是一门面向对象的编程语言吗?这要看你问谁(以及你是如何表述的)。
不过,普遍的共识是肯定的,但也有一些需要注意的地方。
在学术界之外,面向对象编程语言通常围绕类、接口和继承等概念展开。可是直到
最近,
JavaScript
还是一个异类,一门建立在函数和原型之上的面向对象编程语言。
随着
ES6
的出现,突然之间,类变成了这门语言的原生结构,水又浑浊了起来。类
仅仅是一种语法糖,还是语言的巨大进步?
答案介于两者之间。总的来说,
ES6
类是一种高级语言特性,建立在我们熟悉的
JavaScript
原型之上。但是二者之间没有对等关系,类模型引入了一些新的特性,与
原型模型相比有微妙的差异,不完全相同。此外,类在未来可能会支持新的面向对
象特性,进一步拉开两种模型的差距。
目前的状况是,新项目开发喜欢使用类,但是基于原型的代码依然常见(而且远未
过时)。本章重点介绍类的常用模式,不过也涉及原型。
8.1
创建一个可重用的类
8.1.1
问题
你想为自定义的对象创建一个可重用的模板。
202
8
8.1.2
方案
使用
class
关键字,为类起一个名称。在类内部添加一个构造函数,用于初始化对象。
下面是一个完整的
Person
类示例。
class
Person {
constructor(firstName, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

JavaScript Patterns

JavaScript Patterns

Stoyan Stefanov
JavaScript

JavaScript

David Flanagan
JavaScript

JavaScript

T. J. Crowder

Publisher Resources

ISBN: 9787519869601