Javascript 在条件内对对象求值为false

Javascript 在条件内对对象求值为false,javascript,Javascript,假设我们已经定义了一个队列对象,并且希望在队列中有项目时循环 显而易见的解决方案是: var queue = new Queue(); // populate queue while (queue.size()) { queue.pop(); } 所需形式: var queue = new Queue(); // populate queue while (queue) { // should stop when queue's size is 0 queue.pop(); }

假设我们已经定义了一个队列对象,并且希望在队列中有项目时循环

显而易见的解决方案是:

var queue = new Queue();
// populate queue
while (queue.size()) {
    queue.pop();
}
所需形式:

var queue = new Queue();
// populate queue
while (queue) { // should stop when queue's size is 0
    queue.pop();
}

有可能实现第二个javascript示例中显示的这种(精确的)语法吗?如果是,怎么做?

它必须是对象吗?为什么不使用数组呢

var queue = [array,of,things,to,do];
while (queue.length) {
    var todo = queue.pop();
    //do something to todo
}

它必须是一个对象吗?为什么不使用数组呢

var queue = [array,of,things,to,do];
while (queue.length) {
    var todo = queue.pop();
    //do something to todo
}

这两种方法中的任何一种都会起作用:

销毁队列,从而满足条件要求

var queue = new Queue();
while (queue) {
  queue.size() ? queue.pop() : queue = null;  
}​
手动断开循环

var queue = new Queue();
while (queue) {
  queue.size() ? queue.pop() : break;  
}​

这两种方法中的任何一种都会起作用:

销毁队列,从而满足条件要求

var queue = new Queue();
while (queue) {
  queue.size() ? queue.pop() : queue = null;  
}​
手动断开循环

var queue = new Queue();
while (queue) {
  queue.size() ? queue.pop() : break;  
}​

像这样的方法应该会奏效:

function Queue() {}
Queue.prototype.toString = function() {
    // I'm using "this.queue" as if it were an array with your actions
    return !!this.queue.length;
};

var queue = new Queue();

// Populate queue

while ( queue ) {
    queue.pop();
}

其思想是重写
toString
以返回一个布尔值,而不是一些字符串值。

类似的操作应该可以:

function Queue() {}
Queue.prototype.toString = function() {
    // I'm using "this.queue" as if it were an array with your actions
    return !!this.queue.length;
};

var queue = new Queue();

// Populate queue

while ( queue ) {
    queue.pop();
}
其思想是重写
toString
,以返回一个布尔值,而不是一些字符串值

有可能实现这种(精确的)语法吗

我的回答是:没有

我试着用下面的方法来解答这个谜语,但似乎不起作用。
然而,我认为这是一条路要走

免责声明:这只是一些解谜游戏,而不是真实世界的代码

var Queue = function () {};
Queue.prototype.sizeValue = 2;
Queue.prototype.size = function () 
{
    return this.sizeValue;
};
Queue.prototype.pop = function () 
{
    // EDIT: yes, it's not popping anything.
    // it just reduces size to make toString()
    // and valueOf() return nulls.
    this.sizeValue -= 1;
};
Queue.prototype.valueOf = function () 
{
    if (this.size() > 0) {
        return this.sizeValue;
    }
    return null;
};
Queue.prototype.toString = function () 
{
    if (this.size() > 0) {
        return "Queue["+this.sizeValue+"]";
    }
    return null;
};


var test = new Queue();
while (test) {
    test.pop();
    if (test.size() < -1) {
        // just to get you out of the loop while testing
        alert("failed");
        break;
    }
}
alert("out:"+test);
var Queue=function(){};
Queue.prototype.sizeValue=2;
Queue.prototype.size=函数()
{
返回此.sizeValue;
};
Queue.prototype.pop=函数()
{
//编辑:是的,它没有弹出任何东西。
//它只是缩小了大小以使toString()成为
//和valueOf()返回空值。
this.sizeValue-=1;
};
Queue.prototype.valueOf=函数()
{
if(this.size()>0){
返回此.sizeValue;
}
返回null;
};
Queue.prototype.toString=函数()
{
if(this.size()>0){
返回“Queue[”+this.sizeValue+“]”;
}
返回null;
};
var test=新队列();
while(测试){
test.pop();
如果(测试尺寸()<-1){
//只是为了让您在测试时脱离循环
警报(“失败”);
打破
}
}
警报(“输出:+测试);
将警报放在toString()和valueOf()中,以查看它们不会被条件
while(test){}
触发

有可能实现这种(精确的)语法吗

我的回答是:没有

我试着用下面的方法来解答这个谜语,但似乎不起作用。
然而,我认为这是一条路要走

免责声明:这只是一些解谜游戏,而不是真实世界的代码

var Queue = function () {};
Queue.prototype.sizeValue = 2;
Queue.prototype.size = function () 
{
    return this.sizeValue;
};
Queue.prototype.pop = function () 
{
    // EDIT: yes, it's not popping anything.
    // it just reduces size to make toString()
    // and valueOf() return nulls.
    this.sizeValue -= 1;
};
Queue.prototype.valueOf = function () 
{
    if (this.size() > 0) {
        return this.sizeValue;
    }
    return null;
};
Queue.prototype.toString = function () 
{
    if (this.size() > 0) {
        return "Queue["+this.sizeValue+"]";
    }
    return null;
};


var test = new Queue();
while (test) {
    test.pop();
    if (test.size() < -1) {
        // just to get you out of the loop while testing
        alert("failed");
        break;
    }
}
alert("out:"+test);
var Queue=function(){};
Queue.prototype.sizeValue=2;
Queue.prototype.size=函数()
{
返回此.sizeValue;
};
Queue.prototype.pop=函数()
{
//编辑:是的,它没有弹出任何东西。
//它只是缩小了大小以使toString()成为
//和valueOf()返回空值。
this.sizeValue-=1;
};
Queue.prototype.valueOf=函数()
{
if(this.size()>0){
返回此.sizeValue;
}
返回null;
};
Queue.prototype.toString=函数()
{
if(this.size()>0){
返回“Queue[”+this.sizeValue+“]”;
}
返回null;
};
var test=新队列();
while(测试){
test.pop();
如果(测试尺寸()<-1){
//只是为了让您在测试时脱离循环
警报(“失败”);
打破
}
}
警报(“输出:+测试);

将警报放在toString()和valueOf()中,以查看它们不会被条件
触发,而(test){}

我使用队列作为示例-实际对象比这更复杂。与其将队列看作堆,不如将其看作堆。空数组是一个真实值(与非空数组一样),因此这是一个无限循环。我以队列为例,实际对象比这更复杂。与其排队,不如把它看作一个堆。空数组是一个真实值(与非空数组一样),因此这是一个无限循环。请参阅我关于使用自定义对象的相关答案。免责声明:可能对您的问题没有直接帮助。我想我的问题还不够清楚。我希望问的是,是否可以定义我的类,以便第二个代码可以在不做任何更改的情况下工作。:)我将试图澄清我的问题。@Macobo:对对象的引用只有在设置为
null
undefined
时才能返回“false”值。就我所知,你所尝试的是不可能的。谢谢你,罗宾-这里的评论(尽管是以一种迂回的方式)在前面向我解释了这一点。请参阅我关于使用自定义对象的相关答案。免责声明:可能对您的问题没有直接帮助。我想我的问题还不够清楚。我希望问的是,是否可以定义我的类,以便第二个代码可以在不做任何更改的情况下工作。:)我将试图澄清我的问题。@Macobo:对对象的引用只有在设置为
null
undefined
时才能返回“false”值。就我所知,你所尝试的是不可能的。谢谢你,罗宾-这里的评论(尽管是以一种跑动的方式)在前面向我解释了这一点。你为什么要这样做?为了实现这一点,您必须销毁对象,因为对象总是真实的,所以您必须将其设置为null或undefined。您为什么要这样做?你必须破坏你的对象来实现这一点,因为对象总是真实的,所以你必须将它设置为null或undefined。唯一不好的是它不工作(在FF和IE中测试)。我只是尝试创建一个类,在prototype中覆盖
toString()
valueOf()
。它们似乎都不是用
while(test){}
子句调用的,这个想法非常简单。唯一不好的是它不工作(在FF和IE中测试)。我只是尝试创建一个类,在prototype中覆盖
toString()
valueOf()
。两个都不是