gpt4 book ai didi

javascript - 覆盖 toString 函数

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

function toString(obj) {
return {}.toString.call(obj);
}

我在一些开源 javascript 项目中看到了这个功能。这个函数的目的是什么?

最佳答案

让我们依次处理每个组件:

return: 返回 call 函数返回的任何值。

{}:这声明了一个空对象。

.toString:每个对象的toString函数。

.call:方法调用具有给定 this 值的函数(参见 MDN Documentation )。在这种情况下,this 是传递给 toString 函数的 obj

关于javascript - 覆盖 toString 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29640270/

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