传递参数时获取工厂函数的名称(奇怪)
Get the name of a factory function when passed with parameters (weird)
好的,我有一个问题。
简短版本:
我想这样做:
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)
}
我尝试过类似这样的事情:
const createThing = function( behaviours(intensities) ){
return {
for(a in arguments){
[a.name]: a;
}
}
}
在过去的一周里,我尝试了很多不同的方法来做到这一点,但都没有成功。谁能帮帮我?
长版:
好的,我有一个磁铁行为工厂函数和一个粒子工厂函数,看起来像这样
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
}
问题是我无法使行为部分起作用。到目前为止,我有一个磁性行为工厂,但我还想要一个电的、引力的、随机的等。我希望粒子对象将行为名称作为新的 属性 键,并将它创建的对象作为此 属性 将此行为作为参数传递给粒子工厂函数时的值,如下所示:
const particle1 = createParticle ( location, dynamic, magnet(intensity) )
//should return
{
location,
dynamic,
magnet: magnet(intensity)
}
甚至
const particle2 = createParticle ( location, dynamic, magnet(intensity), eletric(intensity) )
//should return
{
location,
dynamic,
magnet: magnet(intensity),
eletric: eletric(intensity)
}
等等。
我尝试使用方法 function.name,但这是不可能的,因为当我将行为函数作为参数传递给粒子时,它会评估为一个对象。我尝试使用回调函数,然后使用 function.name 但它没有做任何事情,因为我仍然需要将行为函数及其参数传递给粒子工厂。
这可能吗???如何???
不,这是不可能的。除非 cat
/dog
/bird
/magnet
/eletric
所有 return 包含相应工厂名称的对象。
特别是:
function createParticle(...args) {
const particle = { kind: 'particle' };
for (const element of args)
particle[element.kind] = element;
return particle;
}
如果您正在使用 classes/constructors+原型,您当然可以使用隐式 .constructor.name
而不是我在上面示例中选择的 .kind
属性。
好的,我有一个问题。
简短版本: 我想这样做:
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)
}
我尝试过类似这样的事情:
const createThing = function( behaviours(intensities) ){
return {
for(a in arguments){
[a.name]: a;
}
}
}
在过去的一周里,我尝试了很多不同的方法来做到这一点,但都没有成功。谁能帮帮我?
长版: 好的,我有一个磁铁行为工厂函数和一个粒子工厂函数,看起来像这样
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
}
问题是我无法使行为部分起作用。到目前为止,我有一个磁性行为工厂,但我还想要一个电的、引力的、随机的等。我希望粒子对象将行为名称作为新的 属性 键,并将它创建的对象作为此 属性 将此行为作为参数传递给粒子工厂函数时的值,如下所示:
const particle1 = createParticle ( location, dynamic, magnet(intensity) )
//should return
{
location,
dynamic,
magnet: magnet(intensity)
}
甚至
const particle2 = createParticle ( location, dynamic, magnet(intensity), eletric(intensity) )
//should return
{
location,
dynamic,
magnet: magnet(intensity),
eletric: eletric(intensity)
}
等等。
我尝试使用方法 function.name,但这是不可能的,因为当我将行为函数作为参数传递给粒子时,它会评估为一个对象。我尝试使用回调函数,然后使用 function.name 但它没有做任何事情,因为我仍然需要将行为函数及其参数传递给粒子工厂。
这可能吗???如何???
不,这是不可能的。除非 cat
/dog
/bird
/magnet
/eletric
所有 return 包含相应工厂名称的对象。
特别是:
function createParticle(...args) {
const particle = { kind: 'particle' };
for (const element of args)
particle[element.kind] = element;
return particle;
}
如果您正在使用 classes/constructors+原型,您当然可以使用隐式 .constructor.name
而不是我在上面示例中选择的 .kind
属性。