对象在Coffeescript中是不可变的吗?

时间:2013-06-17 02:23:57

标签: javascript coffeescript compoundjs jugglingdb

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。有什么想法吗?

1 个答案:

答案 0 :(得分: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