您的代码错误在于您如何使用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.tail 是 True,就像当你用一个故事创造一只老鼠时,那么你可以选择事故,如果事故发生,无论如何您传递给 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);