From: Carlos JP on 28 May 2010 13:14 Having a object var a = { name : "Carlos", id : 102 } and a function: function getDetail() { return { adress : "Portugal", phone: 214443534535} } how can i create an object that is the union of the object and the function return something like: var b = a.Join(getDetail()) ??
From: Thomas 'PointedEars' Lahn on 28 May 2010 13:20 Carlos JP wrote: > Having a object > > var a = > { > name : "Carlos", > id : 102 > } > > and a function: > > function getDetail() > { > return { adress : "Portugal", > phone: 214443534535} > } > > > how can i create an object that is the union of the object and the > function return something like: > > var b = a.Join(getDetail()) ?? By creating the corresponding properties on the object referred by `a'. It is not a good idea to ask other people to do your homework. <http://jibbering.com/faq/#posting> PointedEars -- Prototype.js was written by people who don't know javascript for people who don't know javascript. People who don't know javascript are not the best source of advice on designing systems that use javascript. -- Richard Cornford, cljs, <f806at$ail$1$8300dec7(a)news.demon.co.uk>
From: Stefan Weiss on 28 May 2010 13:30 On 28/05/10 19:14, Carlos JP wrote: > Having a object > > var a = > { > name : "Carlos", > id : 102 > } > > and a function: > > function getDetail() > { > return { adress : "Portugal", > phone: 214443534535} > } > > > how can i create an object that is the union of the object and the > function return something like: > > var b = a.Join(getDetail()) ?? There's no built-in method for this, but you can write a short utility function. You'll have to decide what you want to copy (include or exclude properties from the source object's prototype?) and how you handle collisions (overwrite? ignore?). Here's an (untested) simple example which ignores the source object's prototype and will always overwrite the target object's properties (and possibly shadow its prototype's properties). function mergeObjects (target, source) { for (var prop in source) { if (source.hasOwnProperty(prop)) { target[prop] = source[prop]; } } } Note that if any of the source objects's properties aren't enumerable, they will not be copied over. -- stefan
From: Carlos JP on 28 May 2010 13:41 thank you, but since your answer didn't help much I'll rephrase my question: I have an object A And a function that returns Object B. I want to create a object C that is the union of the two. I can iterate through the properties in A like this for(prop in B) { //Add prop to A } What should I do to add the pop to A? I don't know in advance what properties object B will have. Thank you.
From: Carlos JP on 28 May 2010 13:43
On May 28, 6:30 pm, Stefan Weiss <krewech...(a)gmail.com> wrote: > On 28/05/10 19:14, Carlos JP wrote: > There's no built-in method for this, but you can write a short utility > function. You'll have to decide what you want to copy (include or > exclude properties from the source object's prototype?) and how you > handle collisions (overwrite? ignore?). > > Here's an (untested) simple example which ignores the source object's > prototype and will always overwrite the target object's properties (and > possibly shadow its prototype's properties). > > function mergeObjects (target, source) { > for (var prop in source) { > if (source.hasOwnProperty(prop)) { > target[prop] = source[prop]; > } > } > } thank you very very much. |