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]);
S
ScriptyChris484d 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)=>{}
};
OR
Owen Rossi-Keen484d ago
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
S
ScriptyChris484d 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;
OR
Owen Rossi-Keen483d ago
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
S
ScriptyChris483d 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 😛
UU
Unknown User483d ago