gpt4 book ai didi

JavaScript .toString() 默认值

转载 作者:行者123 更新时间:2023-11-28 18:46:14 24 4
gpt4 key购买 nike

我正在尝试编写一个函数,该函数将另一个函数作为参数(我们可以假设另一个函数的 toString 方法将始终被覆盖并返回自定义值)并返回该函数的原始 toString 值。

     function foo () {} 
foo.toString = function () {
return 'abc'; }

如何恢复函数的 toString 方法,使其再次返回“function foo () {}”?

最佳答案

由于 toString 是一个原型(prototype)属性,当您删除它时,它会恢复为默认值:

function foo () {} 

foo.toString = function () {
return 'abc';
}

document.write('<pre>'+JSON.stringify(String(foo),0,3));

delete foo.toString

document.write('<pre>'+JSON.stringify(String(foo),0,3));

关于JavaScript .toString() 默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35299159/

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