object1 = {
"Name": "foo",
"age": 20
};
object2 = {
"name": "someone",
"age": 21
};
function person() {
console.log(`My self ${name} i am ${age} old`);
}
person();
Here, I just want to pass different objects as a parameter of the same function. When I click a button object1 will be passed. When I click another button object2 will be passed. But the problem is without changing a literal expression [like object1.name to object2.name] to execute this function
CodePudding user response:
const object1 = {
"name": "foo",
"age": 20
};
const object2 = {
"name": "someone",
"age": 21
};
function person({name, age}) {
console.log(`My self ${name} i am ${age} old`);
}
person(object1);
person(object2);
CodePudding user response:
you can call person(object1) or person(object2), and your function can be something like :
function person(obj) {
console.log(`My self ${obj.name} i am ${obj.age} old`);
}