R
Reactiflux

help-js

ekomS – 23-57 Dec 21

OROwen Rossi-Keen12/21/2022
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]);
SScriptyChris12/22/2022
@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)=>{}
};
OROwen Rossi-Keen12/22/2022
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
SScriptyChris12/22/2022
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;
OROwen Rossi-Keen12/22/2022
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
SScriptyChris12/22/2022
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 😛
UUUnknown User12/22/2022
Message Not Public
Sign In & Join Server To View

Looking for more? Join the community!