在传递(作为其参数)动态值的函数内使用绑定,该动态值将由绑定方法本身使用
Using bind inside a function that passes (as its argument) a dynamic value to be used by the bind method itself
我正在练习应用、调用和绑定方法,我正在尝试在以下上下文中使用绑定:
const chardonnay = {
type: "still white",
grape: "chardonnay",
region: "France",
description: function () {
return `This is a ${this.type} wine made of 100% ${this.grape} grapes from ${this.region}.`;
},
};
const malbec = {
type: "still red",
grape: "malbec",
region: "Argentina",
};
const describeWine = (wine) => {
return chardonnay.description.bind(wine);
};
console.log(describeWine(malbec));
基本上我想要完成的是我可以传递一个动态值供绑定稍后使用,以便我将绑定功能存储在另一个函数上,该参数将是绑定函数方法的参数。
对不起,这是我能解释的最好的了。
谁能解释为什么这种方法没有给我想要的结果,什么是实现该结果的最佳方法?
您忘记像这样调用绑定的返回函数:
const chardonnay = {
type: "still white",
grape: "chardonnay",
region: "France",
description: function () {
return `This is a ${this.type} wine made of 100% ${this.grape} grapes from ${this.region}.`;
},
};
const malbec = {
type: "still red",
grape: "malbec",
region: "Argentina",
};
const describeWine = (wine) => {
return chardonnay.description.bind(wine)();
};
console.log(describeWine(malbec));
我正在练习应用、调用和绑定方法,我正在尝试在以下上下文中使用绑定:
const chardonnay = {
type: "still white",
grape: "chardonnay",
region: "France",
description: function () {
return `This is a ${this.type} wine made of 100% ${this.grape} grapes from ${this.region}.`;
},
};
const malbec = {
type: "still red",
grape: "malbec",
region: "Argentina",
};
const describeWine = (wine) => {
return chardonnay.description.bind(wine);
};
console.log(describeWine(malbec));
基本上我想要完成的是我可以传递一个动态值供绑定稍后使用,以便我将绑定功能存储在另一个函数上,该参数将是绑定函数方法的参数。
对不起,这是我能解释的最好的了。
谁能解释为什么这种方法没有给我想要的结果,什么是实现该结果的最佳方法?
您忘记像这样调用绑定的返回函数:
const chardonnay = {
type: "still white",
grape: "chardonnay",
region: "France",
description: function () {
return `This is a ${this.type} wine made of 100% ${this.grape} grapes from ${this.region}.`;
},
};
const malbec = {
type: "still red",
grape: "malbec",
region: "Argentina",
};
const describeWine = (wine) => {
return chardonnay.description.bind(wine)();
};
console.log(describeWine(malbec));