Connecting Tech Pros Worldwide Forums | Help | Site Map

How do I create a function that copies all the fields?

disappearedng
Guest
 
Posts: n/a
#1: Nov 13 '08
Hi everyone
It's me again,
this time I am trying to write a function that copies all the fields
from one object to another and supplementing all these fields with the
value null

How do I go about it?

var myMammal = {
name : 'Herb the Mammal',
get_name : function() {
return this.name;
},
says : function() {
return this.saying || '' ;
}
};

var copyfields = function(fromZ, toZ){
for (var name in fromZ)
toZ[name] = "null";
};

Firebug complains about toZ not defined. I understand that this is
because of toZ is undefined from the perspective of each "name" in
fromZ.

How do I go about this?
Erwin Moller
Guest
 
Posts: n/a
#2: Nov 14 '08

re: How do I create a function that copies all the fields?


disappearedng schreef:
Quote:
Hi everyone
It's me again,
this time I am trying to write a function that copies all the fields
from one object to another and supplementing all these fields with the
value null
>
How do I go about it?
>
var myMammal = {
name : 'Herb the Mammal',
get_name : function() {
return this.name;
},
says : function() {
return this.saying || '' ;
}
};
>
var copyfields = function(fromZ, toZ){
for (var name in fromZ)
toZ[name] = "null";
};
>
Firebug complains about toZ not defined. I understand that this is
because of toZ is undefined from the perspective of each "name" in
fromZ.
>
How do I go about this?
Hi,

Maybe show some more code?
And why do you use:
var copyfields = function(fromZ, toZ)
instead of
function copyfields (fromZ, toZ)
?

Are you sure you initialized toZ before calling?
eg: var someZ = new Object();
copyfields (fromZ, someZ);

Regards,
Erwin Moller

--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
William James
Guest
 
Posts: n/a
#3: Nov 21 '08

re: How do I create a function that copies all the fields?


On Nov 13, 12:00*am, disappearedng <disappeare...@gmail.comwrote:
Quote:
Hi everyone
It's me again,
this time I am trying to write a function that copies all the fields
from one object to another and supplementing all these fields with the
value null
>
How do I go about it?
>
var myMammal = {
* * * * name * *: * * * 'Herb the Mammal',
* * * * get_name * * * *: * * * function() * * *{
* * * * * * * * * * * * * * * * * * * * * * * * return this.name;
* * * * * * * * * * * * * * * * * * * * * * * * },
* * * * says * * * * * *: * * * function() * * *{
* * * * * * * * * * * * * * * * * * * * * * * * return this.saying || '' ;
* * * * * * * * * * * * * * * * * * * * * * * * }
>
};
>
var copyfields = function(fromZ, toZ){
* * * * for (var name in fromZ)
* * * * * * * * toZ[name] = "null";
>
};
>
Firebug complains about toZ not defined. I understand that this is
because of toZ is undefined from the perspective of each "name" in
fromZ.
>
How do I go about this?
var my_mammal = {
name : 'Herb the Mammal',
get_name : function() { return this.name },
says : function() { return this.saying || '' }
}

function copy_fields(from_z, to_z)
{
for (var name in from_z)
to_z[name] = null
}

var temp = {}
copy_fields( my_mammal, temp )
Jorge
Guest
 
Posts: n/a
#4: Nov 21 '08

re: How do I create a function that copies all the fields?


On Nov 13, 7:00*am, disappearedng <disappeare...@gmail.comwrote:
Quote:
Hi everyone
It's me again,
this time I am trying to write a function that copies all the fields
from one object to another and supplementing all these fields with the
value null
>
How do I go about it?
>
var myMammal = {
* * * * name * *: * * * 'Herb the Mammal',
* * * * get_name * * * *: * * * function() * * *{
* * * * * * * * * * * * * * * * * * * * * * * * return this.name;
* * * * * * * * * * * * * * * * * * * * * * * * },
* * * * says * * * * * *: * * * function() * * *{
* * * * * * * * * * * * * * * * * * * * * * * * return this.saying || '' ;
* * * * * * * * * * * * * * * * * * * * * * * * }
>
};
>
var copyfields = function(fromZ, toZ){
* * * * for (var name in fromZ)
* * * * * * * * toZ[name] = "null";
>
};
>
Firebug complains about toZ not defined. I understand that this is
because of toZ is undefined from the perspective of each "name" in
fromZ.
>
How do I go about this?
var copyfields= function (fromZ, toZ) {
toZ= (typeof toZ === "object") ? toZ : {};
for (var name in fromZ) {
if (fromZ.hasOwnProperty(name)) {
toZ[name]= null;
}
}
};

--
Jorge.
Closed Thread