calling a function with an arbitrary object's properties

M

Marcin

Hi everyone,
Wondering if anyone has an idea about this.

I need a function (I'll call it MagicFunc) that when passed an object
will be able to unpack all the properties and call a supplied function
with those properties as individual parameters.

Example
var foo = { x: 1, y: 2, z: 3 };
var bar = { username: "john", password: "hardpass" };

magicFunc(foo, displayPoint)
magicFunc(bar, displayUsername);

which will actually translate to
displayPoint(foo.x, foo.y, foo.z);
displayUsername(bar.username, bar.password);

I know I could just change the displayPoint/displayUsername functions
to be something like

function displayPoint( obj )
{
var x = obj.x;
var y = obj.y;
var z = obj.z;
//do stuff
}

function displayUsername( obj )
{
var username = obj.username;
var password = obj.password;
}

but I specifically want to keep displayPoint as displayPoint(x,y,z);

And I don't want to do stuff through arguments either, the point is so
that functions defined like the above functions won't need to be
updated.

Any idea any one?

Much appreciated,
-Marcin
 
M

Martin Honnen

Marcin said:
I need a function (I'll call it MagicFunc) that when passed an object
will be able to unpack all the properties and call a supplied function
with those properties as individual parameters.

Example
var foo = { x: 1, y: 2, z: 3 };
var bar = { username: "john", password: "hardpass" };

magicFunc(foo, displayPoint)
magicFunc(bar, displayUsername);

which will actually translate to
displayPoint(foo.x, foo.y, foo.z);
displayUsername(bar.username, bar.password);


function magicFunc (obj, fun) {
var args = new Array();
for (var prop in obj) {
args.push(obj[prop]);
}
fun.apply(this, args);
}
 
M

Marcin

Marcin said:
I need a function (I'll call it MagicFunc) that when passed an object
will be able to unpack all the properties and call a supplied function
with those properties as individual parameters.
Example
var foo = { x: 1, y: 2, z: 3 };
var bar = { username: "john", password: "hardpass" };
magicFunc(foo, displayPoint)
magicFunc(bar, displayUsername);
which will actually translate to
displayPoint(foo.x, foo.y, foo.z);
displayUsername(bar.username, bar.password);

function magicFunc (obj, fun) {
   var args = new Array();
   for (var prop in obj) {
     args.push(obj[prop]);
   }
   fun.apply(this, args);

}

Thanks so much!

-Marcin
 
R

Richard Cornford

Marcin wrote:
function magicFunc (obj, fun) {
   var args = new Array();
   for (var prop in obj) {
     args.push(obj[prop]);
   }
   fun.apply(this, args);
}
Thanks so much!

Remembering that ECMAScript 3re Ed. - for-in - iteration does not
guarantee the order of the property names enumerated. So this can
never be guaranteed to be more cross-browser than being functional in
the browsers where it can be observed to work.

Richard.
 
J

Jorge

Marcin said:
I need a function (I'll call it MagicFunc) that when passed an object
will be able to unpack all the properties and call a supplied function
with those properties as individual parameters.
Example
var foo = { x: 1, y: 2, z: 3 };
var bar = { username: "john", password: "hardpass" };
magicFunc(foo, displayPoint)
magicFunc(bar, displayUsername);
which will actually translate to
displayPoint(foo.x, foo.y, foo.z);
displayUsername(bar.username, bar.password);

function magicFunc (obj, fun) {
   var args = new Array();
   for (var prop in obj) {
     args.push(obj[prop]);
   }
   fun.apply(this, args);
}

function magicFunc (obj, fun) {
var prop, args = [];
for ( prop in obj ) {
if ( obj.hasOwnProperty(prop) ) {
args.push(obj[prop]);
}
}
fun.apply(this, args);
}
 

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

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top