该方法可从传入的JSON对象创建一个 Vehicle
的实例。
Vehicle.fromJSON(json)
json
:对象,必需。要创建 Vehicle
实例的JSON对象。const json = {
make: "Toyota",
model: "Camry",
year: 2020,
color: "blue"
};
const vehicle = Vehicle.fromJSON(json);
Vehicle
实例。
json
对象至少应包含 make
,model
,year
和 color
的属性。year
属性应该是整数类型。const json = {
make: "BMW",
model: "X5",
year: 2018,
color: "silver"
}
const car = Vehicle.fromJSON(json);
console.log(car.make); // "BMW"
console.log(car.model); // "X5"
console.log(car.year); // 2018
console.log(car.color); // "silver"