function Car(color) {
this.color = color;
this.drive = function() {
console.log("The " + this.color + " car is driving.");
};
}
const myCar = new Car('red');
myCar.drive(); // 输出: The red car is driving.
// 解释:
// 在这个例子中, `this` 指向的是当前的 Car 实例 (myCar)。
// 当我们调用 `myCar.drive()` 时,`this` 就指向了 `myCar`,因此可以访问到 `myCar` 的属性 `color`。
上一篇:text/javascript
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站