[英]How can I subclass Boolean to change string representation without negative side effects outside of my function?
I need to a way to wrap a boolean value such that comparisons are not broken and the string result is different than 'false' or 'true' without altering the global boolean values 我需要一种包装布尔值的方法,以使比较不中断并且字符串结果与'false'或'true'不同,而无需更改全局布尔值
function TestIt(bool){
if(wrapper(bool) == true)
return "it was: " + wrapper(bool)
if(wrapper(bool) == false)
return "it was: " + wrapper(bool)
return "no dice"
}
eg 例如
var result;
result = TestIt(true);
// "it was: True"
result = TestIt(false);
// "it was: False"
The attempts I have written have not been able to achieve all of the conditions below at the same time: 我写的尝试无法同时满足以下所有条件:
var initial = true;
var result1;
var result2;
(function(){
result1 = wrapper(true);
result2 = wrapper(true);
})()
// result1 == result2
// result1 == true
// result1.toString() != initial.toString()
// initial.toString() == true.toString()
// initial.toString() == (new Boolean(true)).toString()
Can anyone help me please? 谁能帮我吗?
I need this (automatic) alternate string conversion so that I can duplicate a string created on a server environment using a different language and match it exactly. 我需要这种(自动)备用字符串转换,以便可以使用不同的语言复制在服务器环境中创建的字符串,并将其完全匹配。
~~~~~~ ~~~~~~
Edit 编辑
~~~~~~ ~~~~~~
I forgot to mention that the trouble I am running into is the Boolean "valueOf" method that is called instead of toString (apparently) for string concatenation. 我忘了提到我遇到的麻烦是布尔“ valueOf”方法,该方法代替了字符串串联的toString(显然)。
~~~~~~ ~~~~~~
Edit #2 编辑#2
~~~~~~ ~~~~~~
This would also need to work for false. 这也需要为错误做准备。 I just left that out for brevity. 为了简洁起见,我只是忽略了这一点。 However, wrapper(false) == false gave me a headache. 但是,wrapper(false)== false使我头疼。
~~~~~~ ~~~~~~
Edit Final 编辑决赛
~~~~~~ ~~~~~~
It turns out (in the answers below), that you can't override the default behavior like I wanted if string concatenation is used. 事实证明(在下面的答案中),如果使用字符串串联,则无法覆盖我想要的默认行为。 I am going to work on using an array to solve my problem and then doing custom conversions when I join it back together. 我将使用数组来解决问题,然后将其重新结合在一起时进行自定义转换。 It seems like Javascript requires an oddball approach to a conceptually simple problem. 似乎Javascript需要用奇数方法解决概念上简单的问题。
Code Example for command line: 命令行的代码示例:
function boolish(a){a=new Boolean(a);a.toString=function(){return this.valueOf()?"True":"False"};return a};
boolish(false) == false
boolish(true) == true
boolish(false) + " or " + boolish(true)
[boolish(false) , " or " , boolish(true)].join("~~~~~~~~")
I don't understand why you think you need this to happen automatically via toString()
. 我不明白为什么您认为您需要通过toString()
自动进行此操作。 If you have a "plain" boolean you can (obviously) use it in any comparisons as normal, but if you want to get some non-standard text when concatenated just specify it at the time. 如果您有一个“纯”布尔值,则可以(很显然)像平常一样在任何比较中使用它,但是如果您希望在连接时得到一些非标准的文本,只需在当时指定它即可。 For example, if you wanted "TRUE" and "FALSE" all in caps: 例如,如果要全部使用大写字母“ TRUE”和“ FALSE”:
var myBool1 = false,
myBool2 = true;
alert("The value of myBool1 is: " + (myBool1?"TRUE":"FALSE"));
Or you could write a little function: 或者您可以编写一个小函数:
function boolToString(val) {
return val ? "True enough" : "That's a lie!";
}
alert("The value of myBool2 is: " + boolToString(myBool2));
EDIT According to the following SO question (and answers): valueOf() vs. toString() in Javascript your requirement can't be done. 编辑根据以下SO问题(和答案): Java中的valueOf()vs. toString()无法满足您的要求。 The string concatenation + operator will always end up using valueOf
rather than toString
. 字符串连接+运算符将始终使用valueOf
而不是toString
。 I agree that this is weird. 我同意这很奇怪。 The workaround is to concatenate the strings without using the + operator, but I gather you don't want to do that. 解决方法是在不使用+运算符的情况下连接字符串,但我认为您不想这样做。
You can do something like: 您可以执行以下操作:
function myBool(value) {
this.value = value;
}
myBool.prototype.toString = function() {
return 'The value is ' + (this.value? 'true' : 'false');
}
var x = new myBool(true);
alert(x); // 'The value is true'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.