JavaScript .toString() 默认值

标签 javascript function tostring

我正在尝试编写一个函数,该函数将另一个函数作为参数(我们可以假设另一个函数的 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/

相关文章:

javascript - JavaScript 中的 this 关键字引用

javascript - Onclick 更改图像,jQuery?

javascript - 当 *ngFor 在 Angular 2 中完成时执行一个函数

java - 如何在 Java 中使用 toString 方法将 int 数组转换为 String

java - java 中的 toString - System.out.println 与 Android 中的 log

javascript - 鼠标移开时启动计时器 鼠标悬停时启动计时器 javascript

r - OLS 估计函数

Python:Reduce() 和函数作为函数的参数

c - 在没有指针的函数中使用函数原型(prototype)

类的实例变量的Java默认值未初始化为int的零