是否有某种方法可以像 PHP 一样将 boolean 值转换为字符串?
这样 false
就变成了 ''
(空字符串),而 true
就变成了 1
。
截至目前,true.toString()
变为 "true"
且 false.toString()
变为 "false “
,不是我想要的。
最佳答案
Boolean.prototype.toPHPString = function() {
return this ? '1' : ''
};
会给你
true.toPHPString() // => '1'
false.toPHPString() // => ''
(1 == 1).toPHPString() // => '1'
关于JavaScript按照PHP的做法将 boolean 值转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18893448/