JavaScript:基于对象的语言与自定义类型构建
立即解锁
发布时间: 2025-08-20 01:27:02 阅读量: 1 订阅数: 5 


JavaScript入门与实践指南
### JavaScript:基于对象的语言与自定义类型构建
在 JavaScript 编程中,对象是核心概念,它能表示诸如字符串、日期和数组等各种事物。下面我们将深入探讨 JavaScript 中对象的使用以及如何创建自定义对象类型。
#### 日期对象的操作示例
先来看一个日期对象操作的代码示例:
```javascript
var nowDate = new Date();
nowDate.setHours(9);
nowDate.setMinutes(57);
alert(nowDate);
nowDate.setMinutes(64);
alert(nowDate);
```
在这段代码中,首先声明了 `nowDate` 变量并将其赋值为一个新的 `Date` 对象,该对象包含当前日期和时间。接着,将小时设置为 9,分钟设置为 57,使用 `alert` 框显示日期和时间,此时应显示 9:57。然后将分钟设置为 64,再次使用 `alert` 框显示日期和时间,由于分钟超过了 60,时间会进位到下一个小时,显示为 10:04。如果将小时设置为 23,再将分钟设置为 64,不仅时间会进入下一个小时,日期也会变为下一天。
#### 创建自定义对象类型(引用类型)
JavaScript 提供了许多内置对象,但有时我们需要根据特定需求创建自定义对象。例如,创建一个电影院订票系统,JavaScript 没有内置的电影院订票对象,所以需要自己设计。
引用类型是对象的模板,它由三部分组成:
- 构造函数:每次基于该引用类型创建对象时都会调用的方法,可用于初始化对象属性。即使不传递参数或代码为空,也需要创建构造函数。
- 方法定义:指定对象可以执行的操作。
- 属性:用于存储对象的数据。
下面以电影院订票系统为例,创建一个 `CustomerBooking` 引用类型:
```javascript
function CustomerBooking (bookingId, customerName, film, showDate) {
this.customerName = customerName;
this.bookingId = bookingId;
this.showDate = showDate;
this.film = film;
}
```
这里定义了构造函数,使用 `this` 关键字来引用对象实例的属性。需要注意的是,JavaScript 会在为属性赋值时自动创建该属性,而不会检查其是否存在。为了避免因拼写错误导致创建新属性而难以调试的问题,可以创建属性的获取和设置方法。
以下是为 `CustomerBooking` 类型创建的属性获取和设置方法:
```javascript
CustomerBooking.prototype.getCustomerName = function() {
return this.customerName;
}
CustomerBooking.prototype.setCustomerName = function(customerName) {
this.customerName = customerName;
}
CustomerBooking.prototype.getShowDate = function() {
return this.showDate;
}
CustomerBooking.prototype.setShowDate = function(showDate) {
this.showDate = showDate;
}
CustomerBooking.prototype.getFilm = function() {
return this.film;
}
CustomerBooking.prototype.setFilm = function(film) {
this.film = film;
}
CustomerBooking.prototype.getBookingId = function() {
return this.bookingId;
}
CustomerBooking.prototype.setBookingId = function(bookingId) {
this.bookingId = bookingId;
}
```
创建方法时,使用 `typeName.prototype.methodName = function()` 的方式。
创建引用类型的实例与创建 JavaScript 内置类型的实例类似,使用 `new` 关键字:
```javascript
var firstBooking = new CustomerBooking(1234, "Robert Smith","Raging Bull", "25 July 2004 18:20");
var secondBooking = new CustomerBooking(1244, "Arnold Palmer","Toy Story", "27 July 2004 20:15");
```
使用 `new` 关键字非常重要,若不使用,不会创建新对象,而是向全局 `window` 对象添加属性,且问题难以诊断。
以下是完整的 HTML 页面示例,展示如何使用 `CustomerBooking` 类型:
```html
<html>
<body>
<script type="text/javascript">
// CustomerBooking type
function CustomerBooking(bookingId, customerName, film, showDate) {
this.customerName = customerName;
this.bookingId = bookingId;
this.showDate = showDate;
this.film = film;
}
CustomerBooking.prototype.getCustomerName = function() {
return this.customerName;
}
CustomerBooking.prototype.setCustomerName = function(customerName) {
this.customerName = customerName;
}
CustomerBooking.prototype.getShowDate = function() {
return this.showDate;
}
CustomerBooking.prototype.setShowDate = function(showDate) {
this.showDate = showDate;
}
CustomerBooking.prototype.getFilm = function() {
return th
```
0
0
复制全文
相关推荐










