【问题标题】:Trouble with JS object method not changing a boolean propertyJS对象方法不更改布尔属性的问题
【发布时间】:2018-11-28 11:22:24
【问题描述】:

我在这里的第一个问题,我只是学习JS,所以请耐心等待。

我正在尝试创建一个名为 rat 的对象,您可以在其中更改它是否有尾巴。如果老鼠有尾巴并且没有变化(即意外),那么它应该仍然有尾巴。在其他情况下,它不应该有尾巴(所以如果它没有尾巴并且发生事故,它仍然没有尾巴)。我想要的逻辑是:

logic

目前我得到的代码是这样的:

function animal(name, age, tail) {
    this.name = name;
    this.age = age;
    this.tail = tail;
    this.changeTail = function (tail) {
        tail=true ? this.tail=tail : this.tail=false;
    }
}

var rat = new animal("Arthur", 2, false);
rat.changeTail(true);

document.write(rat.name + '<br />' + rat.age + '<br />has tail: ' + rat.tail);

问题是结果不符合逻辑。当rat.tail 设置为false 并且changeTailtrue 时,我希望它返回false(因为老鼠不能长出另一条尾巴)-但它会返回true

我错过了什么?

【问题讨论】:

  • this.changeTail = function (tail) { tail? this.tail : 假的; }

标签: javascript object methods conditional


【解决方案1】:

您的代码错误在于您如何使用ternary operator,这是我的最爱之一。

伪代码介绍如下:

variable_getting_value = boolean_expression ? value_returned_if_true : value_returned_if_false;

这个词的等价物:

if(boolean_expression)
    return value_returned_if_true;
else
    return value_returned_if_false;

在您的代码中,您似乎认为可以将代码放在三元运算符中,而实际上您必须放入一个返回的值。我已经编辑了代码以显示您实际尝试做的事情。

来自

tail=true ? this.tail=tail : this.tail=false;

this.tail= this.tail ? tail : this.tail;

看到区别了吗?你走在正确的道路上,干得好!另外,我在代码中添加了适当的变量,即您的 this.tail。否则,您只是更改传递给函数的参数的值。

/*
If the rat has its tail and there is no change (i.e. accident), then it should still have its tail. In the other instances it should not have its tail (so if it's got no tail and has an accident, it still has no tail). 
*/

function animal(name, age, tail)
{
	this.name = name;
	this.age = age;
	this.tail = tail;
	this.changeTail = function (tail)
	{
		this.tail= this.tail ? tail : this.tail;
	}
}

var rat = new animal("Arthur", 2, false);
rat.changeTail(true);

document.write(rat.name + '<br />' + rat.age + '<br />has tail: ' + rat.tail);

本质上,在三元运算符的 boolen_expression 中,限制了老鼠是否有尾巴。同样的道理,如果老鼠出事了,以后就没有尾巴了,你也不能加一条。因此,如果老鼠 没有尾巴,它永远不会改变它,因为如果 boolean_expression 将始终返回 False 并且 value_returned_if_false 总是会得到回报,这将是 False。同时,如果 this.tailTrue,就像当你用一个故事创造一只老鼠时,那么你可以选择事故,如果事故发生,无论如何您传递给 changeTail() 的值将是 this.tail 的新值。

潜在的陷阱:

var rat = new animal("Arthur", 2, true);
rat.changeTail(false);

在此代码中,输出将为 False,因为您将传递给函数的值按原样放置。更糟糕的是,如果你传递一个数值,你的 this.tail 也会突然变成一个数字。

var rat = new animal("Arthur", 2, true);
rat.changeTail(5); 

现在 rat.tail 实际上是 5。不完全是预期的对吗?我添加了一个代码 sn-p 来解决这个问题,因为我不理解 changeTail(true) changeTail(false) 和“意外”之间的概念。

/*
    If the rat has its tail and there is no change (i.e. accident), then it should still have its tail. In the other instances it should not have its tail (so if it's got no tail and has an accident, it still has no tail). 
    */

    function animal(name, age, tail)
    {
    	this.name = name;
    	this.age = age;
    	this.tail = tail;
    	this.changeTail = function (tail)
    	{
    		//Check if parameter is not actually boolean
        if (typeof tail != typeof true){
            throw "changeTail only accepts boolean values";
        }
        
        //If you got here, tail is a boolean value, but I don't really understand what should happen if tail is false, so I have taken some liberties with the following
        let accident_occurred = tail && true; 
        //The previous line will return true only if tail is true because of the &&
        //Only makes changes to tail if accident_occurred
        this.tail= this.tail && accident_occurred ? false : this.tail;
        //I put in a solid false value because I am assuming the tail parameter actually means that an accident occurred and you want the tail cut    
    	}
    }
    
    animal.prototype.toString = function()
{
    return this.name + '<br />' + this.age + '<br />has tail: ' + this.tail + '<br /><br />';
}

    var rat1 = new animal("Arthur", 2, false);
    rat1.changeTail(true);
    
    var rat2 = new animal("Rosy", 2, true);
    rat2.changeTail(false);
    
    document.write(rat1.name + " change tail true <br/><br/>");
    document.write(rat1);
    document.write(rat2.name + " change tail false <br/><br/>");
    document.write(rat2);
    
    document.write(rat1.name + " change tail false <br/><br/>");
    rat1.changeTail(false);
    document.write(rat1);
    
    document.write(rat2.name + " changet tail true <br/><br/>");
    rat2.changeTail(true);
    document.write(rat2);

【讨论】:

  • 那行得通 :) 谢谢!我仍然对...有点困惑 编辑:不,我明白了。第二个“这条尾巴”是检查的内容,结果由它决定。优秀的。非常感谢您为我解决这个问题。
  • 没问题,这就是 Stackoverflow 的用途。 :)
【解决方案2】:

function animal(name, age, tail)
{
	this.name = name;
	this.age = age;
	this.tail = tail;
	this.changeTail = function (tail)
	{
		this.tail=tail ? false: this.tail;
	}
}

var rat = new animal("Arthur", 2, true);   //initially, tail is intact
var mat = new animal("Matt", 3, false);    //initially, has not tail

console.log(rat.name + ' age: ' + rat.age + ', has tail: ' + rat.tail);
console.log(mat.name + ' age: ' + mat.age + ', has tail: ' + mat.tail);

rat.changeTail(true);                     // accident!
mat.changeTail(true);

console.log(rat.name + ' age: ' + rat.age + ', has tail: ' + rat.tail);
console.log(mat.name + ' age: ' + mat.age + ', has tail: ' + mat.tail);
function (tail) {
    tail=true ? this.tail=tail : this.tail=false;
}

此函数不返回任何值,也不影响实例属性。它只作用于传递给它的参数(按值,而不是引用)。

解决方法如下:

  function (tail) {
         this.tail=tail ? false : this.tail;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-09
    • 2021-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多