我在这里的第一个问题,我只是在学习JS,所以请耐心等待。
我正在尝试创建一个名为rat的对象,您可以在其中更改是否带有尾巴。如果老鼠有尾巴并且没有任何变化(即意外),那么它应该仍然有尾巴。在其他情况下,它不应该有尾巴(因此,如果它没有尾巴并且发生事故,那么它仍然没有尾巴)。我希望它的逻辑是:
目前我得到的代码是这样的:
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);
问题是结果不符合逻辑。我希望当false
设置为rat.tail
且false
为changeTail
时,它会返回true
(因为老鼠不能再长尾巴了)-但是它会返回true
。
我想念什么?
答案 0 :(得分:0)
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;
}
答案 1 :(得分:0)
您的代码错误在于您如何使用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 和< strong> 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);
现在鼠尾巴实际上是 5 。不完全是预期的对吗?由于我不了解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);