Join Javascript objects

C

Carlos JP

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()) ??
 
T

Thomas 'PointedEars' Lahn

Carlos said:
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
 
C

Carlos JP

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.
 
C

Carlos JP

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.
 
A

Asen Bozhilov

Carlos said:
how can i create an object that is the union of the object and the
function return something like:

var b = a.Join(getDetail()) ??

What is expected value returned by `Join' method? New native object or
will modify the object referred by `a'? One possible approach is to
create new object which has in prototype chain reference to `a' and
augment that object with new properties.

var objectCreate = (function() {
function F(){}
return function(obj) {
F.prototype = obj;
return new F();
};
})();

var person = {
name : "Carlos",
id : 102
};

var personInfo = objectCreate(person);
personInfo.adress = "Portugal";
personInfo.phone = 214443534535;


print(personInfo.name);
print(personInfo.id);
print(personInfo.adress);
print(personInfo.phone);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,077
Messages
2,570,569
Members
47,206
Latest member
MalorieSte

Latest Threads

Top