JS object 方法的问题不改变布尔值 属性

Trouble with JS object method not changing a boolean property

我的第一个问题,刚学JS,请耐心等待

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

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 设置为 falsechangeTailtrue 时,我希望它为 return false(因为一只老鼠不能长出另一只tail) - 但它 returns true 相反。

我错过了什么?

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

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

修复方法如下:

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

您的代码错误在于您如何使用 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;

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

来自

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 将永远 return a Falsevalue_returned_if_false 将始终得到 returns,这将是 False 。同时,如果 this.tailTrue,就像当你用故事创造一只老鼠时,那么你可以选择意外,如果意外发生,无论你传递给 changeTail() 的值将是 this.tail.[=21 的新值=]

潜在陷阱:

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。不完全是预期的对吗?我添加了一个代码片段来解决这个问题,因为我不理解 changeTail(true) changeTail(false) 和 "accident".

之间的概念

/*
    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);