Connecting Tech Pros Worldwide Help | Site Map

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

  #1  
Old November 13th, 2008, 06:05 AM
disappearedng
Guest
 
Posts: n/a
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?
  #2  
Old November 14th, 2008, 10:35 AM
Erwin Moller
Guest
 
Posts: n/a

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
  #3  
Old November 21st, 2008, 10:15 PM
William James
Guest
 
Posts: n/a

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 )
  #4  
Old November 21st, 2008, 11:05 PM
Jorge
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 09:56 PM
How do I create pdf reports from Access? dog answers 7 November 13th, 2005 07:06 AM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 03:15 AM
Sorting 'n grouping records in a report = easy. How about trying to sort 'n group those same recs fed to a Function??? MLH answers 1 November 12th, 2005 02:30 PM