I have this code
let someFunc1 = () => {
const name1 = callFn({"k1": "v1", "k2": "v2"})
const name2 = callFn({"k1.1": "v1.1", "k2.1": "v2.1"})
const name3 = callFn({"k1.2": "v1.2", "k2.2": "v2.2"})
}
let someFunc2 = () => {
const name4 = callFn({"k1": "v1", "k2": "v2"})
const name5 = callFn({"k1.1": "v1.1", "k2.1": "v2.1"})
const name6 = callFn({"k1.2": "v1.2", "k2.2": "v2.2"})
}
I'd like to add the following behaviour to all of these "someFunc" functions:
- Take a json object argument of the form { : {k1: v1}, : {k2: v2}, ... } which finds a variable (if it exists) with the same name as and modifies the arguments to that callFn (if exists then update it to , else create a new key-value pair)
Desired behaviour and outcome examples:
eg 1. If I wish to change the parameters to the callFn returning the variable "name2" by updating "k2.1" to "myNewValue" I would then be able to call someFunc1( { "name2": {"k2.1": "myNewValue"} } )
, which would effectively change someFunc1
to read:
let someFunc1 = () => {
const name1 = callFn({"k1": "v1", "k2": "v2"})
const name2 = callFn({"k1.1": "v1.1", "k2.1": "myNewValue"})
const name3 = callFn({"k1.2": "v1.2", "k2.2": "v2.2"})
}
eg. 2 To add a new key-value pair ("myNewKey: "myNewValue") to the parameters passed to to the callFn returning the variable named "name6" I would be able to call someFunc2( { "name6": {"myNewKey": "myNewValue"} } )
which would effectively change someFunc2
to read:
let somefunc2 = () => {
const name4 = callfn({"k1": "v1", "k2": "v2"})
const name5 = callfn({"k1.1": "v1.1", "k2.1": "v2.1"})
const name6 = callfn({"k1.2": "v1.2", "k2.2": "v2.2", "myNewKey": "myNewValue"})
}
I'm new to JS to not sure this is possible but if it is, guessing there might be some way to use decorators and/or reflection to achieve this?
Aucun commentaire:
Enregistrer un commentaire