0
this.listPoints: PointCollection
   items: Object
          480: PointObject
          500: PointObject
          700: PointObject
          default: PointObject

I have object listPoints that contains a list items as above. I want to add a new item (e.g. 600) to listPoints and this item contains all data/information of default item, and when changing any information in 600 then information of default is not affected. Please help.

H V
  • 131
  • 2
  • 11
  • 3
    What is this? Coffeescript? – elclanrs Jun 21 '13 at 06:22
  • 1
    @Juhana: Yea, I am using `var new = $.extend(true, {},this.listPoints.items['default'])` to have a copy of `default` but type of `new` is `Object` while I hope type of `new` should be `PointObject` – H V Jun 21 '13 at 06:41
  • If you want to merge the `default` object into a fresh instance, you should do something like `var n = $.extend(true, new PointObject(), this.listPoints.items['default']);` – Niko Jun 21 '13 at 06:58
  • 1
    @Niko: It does not work, because `PointObject` needs some parametters when initiating – H V Jun 21 '13 at 07:12
  • And what prevents you from passing some? – Niko Jun 21 '13 at 07:15
  • 1
    If passing parametters, `new PointObject` will create a new `PointObject` that I don't need. I just need a copy. Another reason is it takes quite lot of time when creating a new `PointObject`, so I use copying an existing item instead of create a new one. – H V Jun 21 '13 at 07:36
  • 1
    Well, you could also use the default object as the prototype of the new one but that doesn't really make a copy (but probably acts as expected most of the time): `var t = function() {}; t.prototype = this.listPoints.items['default']); var n = new t();` – Niko Jun 21 '13 at 07:55

0 Answers0