Okay, so I have a problem.
SHORT VERSION: I want to do this:
const createThing = function( behaviours(intensities) ){
return {
behaviour1: behaviour1(intensity1),
behaviour2: behaviour2(intensity2)
and so on for each behaviour(intensity) passed as an argument
}
}
//so when doing:
const wtf = createThing( cat(loud), dog(whereistheball), bird(dee) );
// wtf should be:
{
cat: cat(loud),
dog: dog(whereistheball),
bird: bird(dee)
}
I've tryied among other things something like this:
const createThing = function( behaviours(intensities) ){
return {
for(a in arguments){
[a.name]: a;
}
}
}
I've been trying a lot of different ways to do this on the past week with no success. Can anyone help me?
LONG VERSION: Okay so I have a magnet behaviour factory function and a particle factory function, it looks like this
const magnet = funtion(intensity) {
// returns a magnetic object with that intensity
}
const createParticle = function( location, static or dynamic, behaviours ){
// returns a particle object with the properties and behaviours listed above
}
The problem is I can't get the behaviours part to work. By now I have a magnetic behaviour factory, but I also want to have an eletrical, gravitacional, random etc. I want the particle object to get the behavior name as a new property key and the object it creates as this property value when this behaviour is passed as an argument to the particle factory function, something like this:
const particle1 = createParticle ( location, dynamic, magnet(intensity) )
//should return
{
location,
dynamic,
magnet: magnet(intensity)
}
or even
const particle2 = createParticle ( location, dynamic, magnet(intensity), eletric(intensity) )
//should return
{
location,
dynamic,
magnet: magnet(intensity),
eletric: eletric(intensity)
}
and so on.
I tried using the method function.name, but it is not possible since when I pass the behaviour function as a parameter to the particle, it evaluates into an object. I tried using a callback function and then using function.name but it doesn't do anything since I still need to pass the behavior function along with its parameters to the particle factory.
Is it even possible??? How???
No, this is not possible. Not unless cat
/dog
/bird
/magnet
/eletric
all return an object that contains the name of the respective factory.
In particular:
function createParticle(...args) {
const particle = { kind: 'particle' };
for (const element of args)
particle[element.kind] = element;
return particle;
}
If you are using classes/constructors+prototypes, you can of course use the implicit .constructor.name
instead of the .kind
property that I chose in the example above.