创建一个 Car 对象,拥有属性name、color、status;拥有方法run,stop,getStatus,利用原型对象的方法
时间: 2024-05-13 11:14:27 浏览: 72
可以这样实现:
```javascript
// 创建 Car 的构造函数
function Car(name, color, status) {
this.name = name;
this.color = color;
this.status = status;
}
// 给 Car 的原型对象添加方法 run
Car.prototype.run = function() {
console.log(this.name + ' is running...');
};
// 给 Car 的原型对象添加方法 stop
Car.prototype.stop = function() {
console.log(this.name + ' is stopped.');
};
// 给 Car 的原型对象添加方法 getStatus
Car.prototype.getStatus = function() {
console.log(this.name + ' is ' + this.status + '.');
};
// 创建 Car 的实例对象
var myCar = new Car('BMW', 'black', 'running');
// 调用 Car 实例对象的方法
myCar.run(); // 输出:BMW is running...
myCar.stop(); // 输出:BMW is stopped.
myCar.getStatus(); // 输出:BMW is running.
```
阅读全文
相关推荐















