如何对 Boolean 进行子类化以更改字符串表示形式,而不会在函数之外产生负面影响?

发布于 2024-12-17 16:51:08 字数 1536 浏览 3 评论 0原文

我需要一种方法来包装布尔值,以便比较不会被破坏,并且字符串结果与“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"
}

例如

var result;
result = TestIt(true);
// "it was: True"
result = TestIt(false);
// "it was: False"

我所写的尝试未能同时实现以下所有条件:

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()

有人可以帮助我吗?

我需要这种(自动)备用字符串转换,以便我可以使用不同的语言复制在服务器环境中创建的字符串并完全匹配。

~~~~~~

编辑

~~~~~~

我忘了提及我遇到的麻烦是调用布尔“valueOf”方法而不是 toString (显然)用于字符串连接。

~~~~~~

编辑 #2

~~~~~~

这也需要为 false 工作。为了简洁起见,我只是省略了它。然而,wrapper(false) == false让我很头疼。

~~~~~~

编辑最终

~~~~~~

事实证明(在下面的答案中),如果使用字符串连接,您无法像我想要的那样覆盖默认行为。我将致力于使用数组来解决我的问题,然后在将其重新连接在一起时进行自定义转换。 Javascript 似乎需要一种奇怪的方法来解决概念上简单的问题。

命令行的代码示例:

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 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

function TestIt(bool){
    if(wrapper(bool) == true)
        return "it was: " + wrapper(bool)
    if(wrapper(bool) == false)
        return "it was: " + wrapper(bool)
    return "no dice"
}

e.g.

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.

~~~~~~

Edit #2

~~~~~~

This would also need to work for false. I just left that out for brevity. However, wrapper(false) == false gave me a headache.

~~~~~~

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.

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("~~~~~~~~")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北渚 2024-12-24 16:51:08

我不明白为什么你认为你需要通过 toString() 自动发生这种情况。如果您有一个“普通”布尔值,您可以(显然)像平常一样在任何比较中使用它,但是如果您想在连接时获得一些非标准文本,只需在当时指定它即可。例如,如果您想要“TRUE”和“FALSE”全部大写:

var myBool1 = false,
    myBool2 = true;

alert("The value of myBool1 is: " + (myBool1?"TRUE":"FALSE"));

或者您可以编写一个小函数:

function boolToString(val) {
   return val ? "True enough" : "That's a lie!";
}

alert("The value of myBool2 is: " + boolToString(myBool2));

编辑根据以下SO问题(和答案):Javascript 中的 valueOf() 与 toString() 您的要求无法完成。字符串连接 + 运算符最终将始终使用 valueOf 而不是 toString。我同意这很奇怪。解决方法是在不使用 + 运算符的情况下连接字符串,但我认为您不想这样做。

I don't understand why you think you need this to happen automatically via 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:

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. The string concatenation + operator will always end up using valueOf rather than 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.

埋葬我深情 2024-12-24 16:51:08

你可以这样做:

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'

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'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文