打印log .log()状态



我正在做一个javascript作业:

给自己写一只虚拟的猫——用CLI的动物比有毛的动物好多了。

创建一个代表猫的对象。它应该有抗疲劳、抗饥饿、抗孤独、抗快乐的特性接下来,编写增加和减少这些属性的方法。给它们取一些能代表增加或减少这些东西的名字,比如"喂食"、"睡眠"或"宠物"。最后,编写一个方法,打印出猫在每个区域的状态。(要有创意,例如:Paws真的很饿,Paws很高兴。)

所以在我的最后一行代码中,我想检查以确保我可以打印状态并查看我得到的响应,但不认为我的最后一个console .log()是正确的。有人能帮我一下吗?另外,你认为这段代码中有什么错误或者我可以缩短它吗?

class Paws {
constructor() {
this.tiredness = 0;
this.hunger = 0;
this.loneliness = 0;
this.happiness = 0;
}
play(tirednessVal, lonelinessVal, hungerVal) {
this.tiredness += tirednessVal;
this.loneliness -= lonelinessVal;
this.hunger += hungerVal;
}
sleep(tirednessVal, lonelinessVal) {
this.tiredness -= tirednessVal;
this.loneliness += lonelinessVal;
}
eat(hungerVal, happinessVal) {
this.hunger -= hungerVal;
this.happiness += happinessVal;
}
getAngry(happinessVal) {
this.happiness -= happinessVal;
}
printStatus() {
const isTired = "The cat is very tired";
const isNotTired = "The cat is not tired";
const isHungry = "The cat is very hungry";
const isNotHungry = "The cat is not hungry";
const isLonely = "The cat is very lonely";
const isNotLonely = "The cat is not lonely";
const isHappy = "The cat is super happy";
const isNotHappy = "The cat is not happy";
if (this.tiredness > 10) {
console.log(isTired);
} else {
console.log(isNotTired);
}
if (this.hunger > 10) {
console.log(isHungry);
} else {
console.log(isNotHungry);
}
if (this.loneliness > 10) {
console.log(isLonely);
} else {
console.log(isNotLonely);
}
if (this._happiness > 10) {
console.log(isHappy);
} else {
console.log(isNotHappy);
}
}
pet() {
if (this.happiness < 5) {
console.log("Meow. Don't touch me!");
} else {
meow();
}
}   
meow() {
console.log("Moew. Pet me more");
}
console.log(Paws.tiredness(4));

一个有趣的任务。这是我的

class Cat {
constructor(name, tiredness = 0, hunger = 10, loneliness = 10, happiness = 0) {
this.name = name;
this.tiredness = tiredness;
this.hunger = hunger;
this.loneliness = loneliness;
this.happiness = happiness;
}
normalizeValue(val) {
if (val < 0) {
return 0;
}
if (val > 10) {
return 10;
}
return val;
}
play(val = 1) {
this.loneliness = this.normalizeValue(this.loneliness - val);
this.happiness = this.normalizeValue(this.happiness + val);
}
feed(val = 1) {
this.hunger = this.normalizeValue(this.hunger - val);
}
pet(val = 1) {
this.loneliness = this.normalizeValue(this.loneliness - val);
}
sleep(val = 1) {
this.tiredness = this.normalizeValue(this.tiredness - val);
}
getStatus() {
return {
tired: this.tiredness > 5,
hungry: this.hunger > 5,
lonely: this.loneliness > 5,
happy: this.happiness > 5
}
}
toString() {
const status = this.getStatus();
return Object.keys(status).map(k => {
const state = status[k] ? "is" : "is not";
return `${this.name} ${state} ${k}`;
}).join(". ") + ".";
}
}
const paws = new Cat("paws");
paws.play(6);
paws.feed(8);
console.log(paws.toString());

您需要实例化Paws类。let paws = new Paws();

关于这段代码有很多要说的。首先,如果在构造函数中添加参数,则可以定义cat的起始属性,但这只是一个细节

constructor(tiredness=0,hunger=0,loneliness=0,happiness=0) {
this.tiredness = tiredness;
this.hunger = hunger;
this.loneliness = loneliness;
this.happiness = happiness;
}

,为了回答你的问题,你应该实例化这个对象。

const paws = new Paws({tiredness : 4});
paws.play(2,4,6)
paws.printStatus();
/*
The cat is not tired
The cat is not hungry
The cat is not lonely
The cat is not happy
*/

@KyleDePace已经回应了这个:

let paws = new Paws();
console.log(paws.tiredness);

正如您所问的缩短代码,在printStatus()中您可以使用三元操作符。

class Paws {
constructor() {
this.tiredness = 0;
this.hunger = 0;
this.loneliness = 0;
this.happiness = 0;
}
play(tirednessVal, lonelinessVal, hungerVal) {
this.tiredness += tirednessVal;
this.loneliness -= lonelinessVal;
this.hunger += hungerVal;
}
sleep(tirednessVal, lonelinessVal) {
this.tiredness -= tirednessVal;
this.loneliness += lonelinessVal;
}
eat(hungerVal, happinessVal) {
this.hunger -= hungerVal;
this.happiness += happinessVal;
}
getAngry(happinessVal) {
this.happiness -= happinessVal;
}
printStatus() {
console.log(this.tiredness > 10 ? "The cat is very tired" : "The cat is not tired");
console.log(this.hunger > 10 ? "The cat is very hungry" : "The cat is not hungry");
console.log(this.loneliness > 10 ? "The cat is very lonely" : "The cat is not lonely");
console.log(this._happiness > 10 ? "The cat is super happy" : "The cat is not happy");
}
pet() {
if (this.happiness < 5)
console.log("Meow. Don't touch me!");
else
meow();
}   

meow() {
console.log("Moew. Pet me more");
}
}

您需要使用new关键字从您的类Paws创建对象。

但是为了让你的方法按照逻辑工作,类的变量必须有一个值,你也可以在对象创建过程中提供。

const paw = new Paws(6,7,9,8); 

class Paws {
constructor(tiredness,hunger,loneliness,happiness) {
this.tiredness = tiredness;
this.hunger = hunger;
this.loneliness = loneliness;
this.happiness = happiness;
}
play(tirednessVal, lonelinessVal, hungerVal) {
this.tiredness += tirednessVal;
this.loneliness -= lonelinessVal;
this.hunger += hungerVal;
}

sleep(tirednessVal, lonelinessVal) {
this.tiredness -= tirednessVal;
this.loneliness += lonelinessVal;
}
eat(hungerVal, happinessVal) {
this.hunger -= hungerVal;
this.happiness += happinessVal;
}
getAngry(happinessVal) {
this.happiness -= happinessVal;
}
printStatus() {
const isTired = "The cat is very tired";
const isNotTired = "The cat is not tired";
const isHungry = "The cat is very hungry";
const isNotHungry = "The cat is not hungry";
const isLonely = "The cat is very lonely";
const isNotLonely = "The cat is not lonely";
const isHappy = "The cat is super happy";
const isNotHappy = "The cat is not happy";
if (this.tiredness > 10) {
console.log(isTired);
} else {
console.log(isNotTired);
}

if (this.hunger > 10) {
console.log(isHungry);
} else {
console.log(isNotHungry);
}

if (this.loneliness > 10) {
console.log(isLonely);
} else {
console.log(isNotLonely);
}

if (this._happiness > 10) {
console.log(isHappy);
} else {
console.log(isNotHappy);
}
}
pet() {
if (this.happiness < 5) {
console.log("Meow. Don't touch me!");
} else {
meow();
}
}   
meow() {
console.log("Moew. Pet me more");
}
}

const paw = new Paws(6,7,9,8); 
console.log(paw.tiredness);

最新更新