473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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?
Nov 13 '08 #1
3 1275
disappearedng schreef:
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
Nov 14 '08 #2
On Nov 13, 12:00*am, disappearedng <disappeare...@gmail.comwrote:
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 )
Nov 21 '08 #3
On Nov 13, 7:00*am, disappearedng <disappeare...@gmail.comwrote:
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.
Nov 21 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Sybren Stuvel | last post by:
Hi people, I'm creating a program that can solve and create Sudoku puzzles. My creation function needs to make a lot of copies of a puzzle. Until now, I used copy.deepcopy(), but that's too...
4
by: Melissa | last post by:
I have a frontend file named CustomerApp and backend file named CustomerData. CustomerApp is at C:\Customer Database and CustomerData is at S:\Customer Database. Could someone help me with the code...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
4
by: Adam - Regus | last post by:
I'm trying to create a button that prints 2 copies of a form. Using the wizard, I created a button that prints one automatically, using the following code: End Sub Private Sub...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
25
by: Licheng Fang | last post by:
I mean, all the class instances that equal to each other should be reduced into only one instance, which means for instances of this class there's no difference between a is b and a==b. Thank...
3
by: DeanL | last post by:
Hi guys, Does anyone know of a way to create multiple tables using information stored in one table? I have a table with 4 columns (TableName, ColumnName, DataType, DataSize) and wanted to know...
0
by: TrevRex | last post by:
Hello, I work for a non-profit in San Diego as a GIS Specialist. I have had to teach myself about some scripting to create some dynamic maps, but I am still very limited in my skills, so I have...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.