【问题标题】:Object is immutable in Coffeescript?对象在 Coffeescript 中是不可变的?
【发布时间】:2013-06-12 22:35:00
【问题描述】:
c.models.car.findOne where: {id: 1}, (err, car)->
    car['seat'] = 1  #seat is not originally in the car object but I would like to add it
    car['color'] = 'red'  #color is originally in car and is changed
    console.log car

问题是颜色正在更改,但没有添加座位。当我执行typeof car 时,它会返回object。有什么想法吗?

【问题讨论】:

  • seat 是汽车属性,但未分配,还是根本就不是属性?
  • 这是数据库查询吗?
  • @Blender 是的,它是一个数据库查询
  • @zeantsoi 座位一开始根本不是一个属性。我想添加它。但它不是“粘”
  • @Alexis:这不是一个真正的 JavaScript 问题。这是你的 ORM 的问题,所以标记它。

标签: javascript coffeescript compoundjs jugglingdb


【解决方案1】:

我认为您正在使用拒绝分配的 ORM。尝试使用这个:

c.models.car.findOne where: {id: 1}, (err, car)->
    car = car.toObject(); # or car = JSON.parse(JSON.stringify(car))
    car['seat'] = 1  #seat is not originally in the car object but I would like to add it
    car['color'] = 'red'  #color is originally in car and is changed
    console.log car

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-21
    • 1970-01-01
    • 2015-12-03
    • 1970-01-01
    相关资源
    最近更新 更多