You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

创建含指定属性与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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 07:10:13