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

D

disappearedng

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?
 
E

Erwin Moller

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
 
W

William James

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

Thomas 'PointedEars' Lahn

disappearedng said:
It's me again,

Thanks for the warning.
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

You have been told already: Objects have *properties*, not fields. Will
you get that now?
How do I go about it?

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

Not using tabs for indentation would be a start. Not using a perversion
of Whitesmiths indentation style here (if you are even aware of that) is
another good idea.
var copyfields = function(fromZ, toZ){
for (var name in fromZ)
toZ[name] = "null";
};

That would copy all *enumerable* properties and set the copy to the *string
value* "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.

Rest assured that you don't understand anything (yet?). `toZ' is defined,
it is the identifier of the second argument. It matters what you pass for
that argument, though, i.e. how you call copyfields(), which is the part of
your code that you omitted. Supposedly you passed nothing or not enough
arguments, and now blame the Firefox script engine for initializing those
arguments as the standard says.
How do I go about this?

Think, read, code, test, think, read, post. In that order.


Score adjusted

PointedEars
 
J

Jorge

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;
}
}
};
 
W

William James

Jorge said:
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;
}
}
};

function map_properties( func, collection )
{ var new_coll = (collection.length+"" === "undefined") ? {} : []
for (var name in collection)
if (collection.hasOwnProperty(name))
new_coll[name] = func( collection[name] )
return new_coll
}

var weights = {foo:22, bar:33, baz:44}
var new_weights = map_properties(function(n){return n*2},weights)

--
 
J

Jorge

function mapProperties (func, collection) {
var newColl= (collection.length+"" === "undefined") ? {} : []

var newColl= new collection.constructor();
  for (var name in collection) {
    if (collection.hasOwnProperty(name)) {
      newColl[name]= func( collection[name] );
}
}
  return newColl;
}
 

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
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top