是否可以操纵 bool 值的字符串化方式?
更改 Boolean.prototype.toString
似乎没有帮助。
这里是我在 mozrepl 和 FF3 中的测试。
repl> Boolean.prototype.toString=function (){return this==true ? "1" : ""}
function() {…}
repl> a.toString()
""
repl> a=true
true
repl> a.toString()
"1"
repl> a+""
"true"
repl> a=false
false
repl> a+""
"false"
我对 ECMA 规范的理解是 +
应该调用 toString()
。
更新:
好的,我找到答案了!
当 ECMA 谈到 ToString()
时它并不意味着 JS 方法 toString()
。
These operators are not a part of the language; they are defined here to aid the specification of the semantics of the language.
参见 http://bclary.com/2004/11/07/#a-9
感谢到目前为止的帮助。
似乎不可能... :(
最佳答案
我想 .toString()
是在原生 bool 类型的 Boolean
版本上调用的(Boolean 是一个 Object 但原生类型不是,与 Numbers 相同) 和 +
被浏览器重载以将 bool 值本地转换为字符串。
以前有人问过这个问题(我很确定我说的就是答案)但我似乎找不到。
关于javascript - 操纵 bool 值的字符串化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7700166/