gpt4 book ai didi

javascript - 如何在对象上正确运行函数以及如何正确使用 .apply() 在对象上运行方法?

转载 作者:行者123 更新时间:2023-11-30 20:26:26 25 4
gpt4 key购买 nike

我无法让它正常运行。我很接近,但事情恰恰与我需要的相反。对此的任何帮助以及对我做错了什么的更多解释将不胜感激。谢谢!!

我得到一个函数 checkOut 和两个对象 book1book2

function checkOut() {
this.available = !this.available;
return `"${this.title}" is ${this.available ? '' : 'not '}available for check-out.`;
}

var book1 = {
title: 'One Fish, Two Fish, Red Fish, Blue Fish',
author: 'Dr. Seuss',
available: false,
checkOut: checkOut
};

var book2 = {
title: "Oh, the Places You'll Go!",
author: 'Dr. Seuss',
available: false
};

var book2CheckOut = checkOut.bind(book2);
console.log(book2CheckOut);

var book2CheckOut2 = book1.checkOut.apply(book2);
console.log(book2CheckOut2);

我首先应该在 book2 上运行函数 checkOut 并将其保存到变量 book2CheckOut

这是我正在尝试的:

var book2CheckOut = checkOut.bind(book2)

这应该给我字符串 "Oh, the Places You'll Go! is available for check-out." 但是我得到 "Oh, the Places You'll Go ! 不可退房。”

然后我应该在 book1 checkOut 函数上使用 .apply() 方法,使用 book2 作为参数运行它。将其保存到变量 book2CheckOut2

这是我正在尝试的:

var book2CheckOut2 = book1.checkOut.apply(book2)

这应该给我字符串 “Oh, the Places You'll Go! is not available for check-out.” 但是给我 “Oh, the Places You'll Go! 可供退房。”

最佳答案

I am first supposed to run the function checkOut on book2 and save it to a variable book2CheckOut.

Here is what I am trying for that:

var book2CheckOut = checkOut.bind(book2)

bind根本不运行该功能。为此,您将使用 apply :

var book2CheckOut = checkOut.apply(book2);

可以为此使用bind,但您想要调用结果,例如:

var checkOutBook2 = checkOut.bind(book2);
var book2CheckOut = checkOutBook2();

var book2CheckOut = checkOut.bind(book2)();

...但是如果您不打算保留和重用它,那么创建函数就没有意义。

Then I am supposed to use the .apply() method on book1 checkOut function, run it using book2 as an argument. Save it to a variable book2CheckOut2.

Here is what I am trying for that:

var book2CheckOut2 = book1.checkOut.apply(book2)

一旦您正确地完成了第一部分(因为第一个调用翻转了 available 标志),它就可以正常工作了。请注意,它与 checkOut.apply(book2) 完全一样,您只是从 book1 上的属性获取函数引用,而不是从范围内标识符 checkout

完整序列:

function checkOut() {
this.available = !this.available;
return `"${this.title}" is ${this.available ? '' : 'not '}available for check-out.`;
}

var book1 = {
title: 'One Fish, Two Fish, Red Fish, Blue Fish',
author: 'Dr. Seuss',
available: false,
checkOut: checkOut
};

var book2 = {
title: "Oh, the Places You'll Go!",
author: 'Dr. Seuss',
available: false
};

var book2CheckOut = checkOut.apply(book2);
console.log(book2CheckOut);

var book2CheckOut2 = book1.checkOut.apply(book2);
console.log(book2CheckOut2);

或者使用我们随后调用的 bind 替代方法:

function checkOut() {
this.available = !this.available;
return `"${this.title}" is ${this.available ? '' : 'not '}available for check-out.`;
}

var book1 = {
title: 'One Fish, Two Fish, Red Fish, Blue Fish',
author: 'Dr. Seuss',
available: false,
checkOut: checkOut
};

var book2 = {
title: "Oh, the Places You'll Go!",
author: 'Dr. Seuss',
available: false
};

var checkOutBook2 = checkOut.bind(book2);
var book2CheckOut = checkOutBook2();
console.log(book2CheckOut);

var book2CheckOut2 = book1.checkOut.apply(book2);
console.log(book2CheckOut2);

关于javascript - 如何在对象上正确运行函数以及如何正确使用 .apply() 在对象上运行方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50863616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com