Home > Blockchain >  How can we pass object as a parameter to a function name in Object value?
How can we pass object as a parameter to a function name in Object value?

Time:05-31

I want to pass an object to a function which is a value of another object. Is this possible, how can we do that? I can pass a string easily and a variable as a string. But, cannot pass as an object.

I have 2 objects like:

var test1 = "qwerty";
var test2 = { qwerty: "sdaf" };

I want to pass these objects as a parameter to an object value like the below.

var props = {
   event: `backdropEvent("${test}")`,
}
function backdropEvent(e){
 console.log(e)
}

When I tried to pass test1 I get it as a string. How can I pass the test2 object?

var test1 = "qwerty";
var test2 = { qwerty: "sdaf" };

var props = {
   event: `backdropEvent("${test2}")`,
}

function backdropEvent(e){
 console.log(e)
}

function testFn(){
testFn2(props);
}

function testFn2(e){
var x = Function(e.event);
x();
}
<button onclick="testFn()"> click me </button>

CodePudding user response:

I may be mistaken since its a little hard to understand what you are trying to accomplish but I beleive that you want to use function binding

const test2 = { qwerty: "sdaf" };

const backdropEvent = (e) => {
    console.log(e);
}

const props = {
    event: backdropEvent.bind(null, test2)
}

props.event()

  • Related