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