pass value from object to another object in javascript
Solution 1:
Like this-
function func(b, c, d) {
return this.a + b + c + d;
}
const small = {
a: 1,
};
const large = {
a: 5,
};
func.call(large, 2,3, 5)
Solution 2:
You can either use call
or apply
here as:
const result = small.func.call(large, 2, 3, 5);
What above statement means is that You are taking the function(or can say borrowing) small.func
function and applying in the context of large
object with argument 2, 3, 5.
const small = {
a: 1,
func: function(b, c, d) {
return this.a + b + c + d;
},
};
const large = {
a: 5,
};
const result = small.func.call(large, 2, 3, 5);
// const result = small.func.apply(large, [2, 3, 5]);
console.log(result);