创建含指定属性与drive方法的car对象的实现及修正
定义Car对象并实现drive方法的问题解决
需求
- 定义变量
car为对象,包含以下属性:model:值为'Nissan'color:值为'Blue'numWheels:数值4
- 对象需包含
drive方法,调用后返回'Vroom!'
尝试的代码
const car = { model: 'Nissan', color: 'Blue', numwheels: 4, drive: function() { return'Vroom!' }, } console.log(car)
执行输出
{ model: 'Nissan', color: 'Blue', numwheels: 4, drive: [Function: drive] }
问题解决
最初在drive方法里使用的是console.log('Vroom!'),无法满足方法返回指定字符串的需求。将其修改为return 'Vroom!'后,就实现了需求要求的效果——调用car.drive()时会返回'Vroom!'。
内容的提问来源于stack exchange,提问作者Imam
相关产品推荐
相关产品推荐

