方式一:使用对象字面量创建car对象

let car = {
  color: "red",
  price: 20000,
  brand: "Toyota",
  start: function() {
    console.log("The car starts.");
  },
  stop: function() {
    console.log("The car stops.");
  }
};

方式二:使用构造函数创建car对象

function Car(color, price, brand) {
  this.color = color;
  this.price = price;
  this.brand = brand;
  this.start = function() {
    console.log("The car starts.");
  };
  this.stop = function() {
    console.log("The car stops.");
  };
}

let car = new Car("blue", 25000, "Honda");
5使用两种方式创建一个汽车car对象。考点:创建对象、添加属性方法属性有:颜色、价格、品牌;方法有:启动、刹车。

原文地址: https://www.cveoy.top/t/topic/bUro 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录