Is there a way to wrap a boolean value in JavaScript so that comparisons remain intact and the resulting string is different from 'false' or 'true', all without changing the global boolean values?
function TestIt(bool){
if(wrapper(bool) == true)
return "it was: " + wrapper(bool)
if(wrapper(bool) == false)
return "it was: " + wrapper(bool)
return "no dice"
}
For example:
var result;
result = TestIt(true);
// "it was: True"
result = TestIt(false);
// "it was: False"
I've been struggling to meet all of these conditions simultaneously with my attempts:
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()
Any help would be greatly appreciated.
I require this alternate string conversion for automatic duplication of server-generated strings in a different language, ensuring an exact match.
~~~~~~
Edit
~~~~~~
I forgot to mention the issue with the Boolean "valueOf" method being called instead of toString for string concatenation.
~~~~~~
Edit #2
~~~~~~
This also needs to work for false. Omitting it earlier caused some difficulties, especially when dealing with wrapper(false) == false.
~~~~~~
Edit Final
~~~~~~
The responses below showed that overriding the default behavior as I intended isn't possible when using string concatenation in Javascript. My plan now is to use an array solution and custom conversions during reassembly. It seems like JavaScript demands a unique approach to what should be a simple problem conceptually.
Command line code example:
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("~~~~~~~~")