ekomS – 23-57 Dec 21

I have an object with nested functions, like so:
const arrayFunctions = {
f1: (a)=> {},
f2: (a,b)=> {},
f3: {
test: (a)=>{}
}
}
const arrayFunctions = {
f1: (a)=> {},
f2: (a,b)=> {},
f3: {
test: (a)=>{}
}
}
Is there any way I can specify default behavior if just arrayFunctions is called? Example:
let a = arrayFunctions.f1([1,2]);
let b = arrayFunctions([4,3,2]);
let a = arrayFunctions.f1([1,2]);
let b = arrayFunctions([4,3,2]);
6 Replies
ScriptyChris
ScriptyChris2y ago
@Smoke arrayFunctions is not a function, so you can't call it. You can make it a function and then attach properties to it
const arrayFunctions = () => { /* do some stuff */ };
arrayFunctions.f1 = (a)=> {};
arrayFunctions.f2 = (a,b)=> {};
arrayFunctions.f3 = {
test: (a)=>{}
};
const arrayFunctions = () => { /* do some stuff */ };
arrayFunctions.f1 = (a)=> {};
arrayFunctions.f2 = (a,b)=> {};
arrayFunctions.f3 = {
test: (a)=>{}
};
Owen Rossi-Keen
Is there any way to nest this assignment within the arrayFunctions function? So that I don't need to write a new line for every deeply nested method
ScriptyChris
ScriptyChris2y ago
No, because you first have to declare a function (which is a special kind of object in JavaScript) and then mutate it by adding properties Though you could have a group of props, which you could define as a single object
const subFunctions = {
f1: (a)=> {},
f2: (a,b)=> {},
f3: {
test: (a)=>{}
},
}
const arrayFunctions = () => { /* do some stuff */ };
arrayFunctions.subFunctions = subFunctions;
const subFunctions = {
f1: (a)=> {},
f2: (a,b)=> {},
f3: {
test: (a)=>{}
},
}
const arrayFunctions = () => { /* do some stuff */ };
arrayFunctions.subFunctions = subFunctions;
Owen Rossi-Keen
turnObjectIntoFunction(object, options) {
let temp = object._default
? function (args) {
return object._default(args);
}
: {};

let keys = Object.keys(object);

for (let index = 0; index < keys.length; index++) {
let key = keys[index];
let data = object[key];

if (key !== '_default')
temp[key] =
typeof data == 'object' && !Array.isArray(data) && options?.recursive
? this.turnObjectIntoFunction(data)
: data;
}

return temp;
},
turnObjectIntoFunction(object, options) {
let temp = object._default
? function (args) {
return object._default(args);
}
: {};

let keys = Object.keys(object);

for (let index = 0; index < keys.length; index++) {
let key = keys[index];
let data = object[key];

if (key !== '_default')
temp[key] =
typeof data == 'object' && !Array.isArray(data) && options?.recursive
? this.turnObjectIntoFunction(data)
: data;
}

return temp;
},
I figured something out This seems to work I just give my desired default behavior the _default key And then parse the object through turnObjectIntoFunction before I export my function/object Fobject? There's probably plenty of ways to optimize but I'm quite possibly braindead after two hours of monkeying around with it
ScriptyChris
ScriptyChris2y ago
I think you overcomplicated this, because it's easier to create an object and append it as function's prop than making some recursion stuff 😛
reactibot
reactibot2y ago
This thread hasn’t had any activity in 12 hours, so it’s now locked. Threads are closed automatically after 12 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1055272921511243896