[英]pass value from object to another object in javascript
const small = { a: 1, func: function(b, c, d) { return this.a + b + c + d; }, }; const large = { a: 5, }; small.func(2, 3, 5);
我需要訪問:5 從大 object 到小 object。 目前 small.func(2,3,5) 將 11 作為 output。 我需要得到 15 作為輸出。
const result = small.func.call(large, 2, 3, 5);
上述語句的意思是您正在使用函數(或者可以說是借用) small.func
function 並在帶有參數 2、3、5 的large
object 的上下文中應用。
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);
像這樣-
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.