如果语句在函数中有效,但在 setter 方法中无效,反之亦然,setter 也根本不起作用
If statement works in a function but not in a setter method, and vice versa, also setter not working at all
我正在 CodeCademy 上做一个项目,我需要为一些学生创建一个 setter 方法。它应该检查该值是否为数字,如果是,则 return 该值,否则记录错误。我最初想出了这个代码
set numberOFStudents(val) {
if(typeof val === 'number') {
this._numberOfStudents = val;
} else {
console.log('Invalid input: numberOfStudents must be set to a Number.')
}
}
但它没有起作用,因为该值没有效果。它将 return 字符串和数字都作为值。
但是,当我在常规函数中使用此方法时,它工作得很好 https://jsfiddle.net/Montinyek/v8zsL5et/12/
演练中使用的方法是这样的:
set numberOfStudents(val) {
if(val.isNaN()) {
console.log('Invalid input: numberOfStudents must be set to a Number.')
} else {
this._numberOfStudents = val;
}
}
这在常规函数中不起作用,因为它 return 是一个错误 https://jsfiddle.net/Montinyek/e6k7qdcs/
但是更让我困惑的是,演练中使用的方法似乎也没有效果。我仍然可以将学生人数设置为一个字符串,它会记录它,而不是记录错误。这是整个代码的 jsfiddle https://jsfiddle.net/Montinyek/1j7wL5gm/
这是因为您没有在构造函数中使用 setter。因此,当您传入字符串时,它会分配给它。
你需要改变
constructor(name, level, numberOfStudents) {
this._name = name;
this._level = level;
this._numberOfStudents = numberOfStudents;
}
到
constructor(name, level, numberOfStudents) {
this._name = name;
this._level = level;
this.numberOfStudents = numberOfStudents;
}
所以它使用 setter 而不是成员变量。第一种方式直接将其分配给您的成员,而不是使用 setter.
这是根据您的示例进行更改的完整代码;
class School {
constructor(name, level, numberOfStudents) {
this._name = name;
this._level = level;
this.numberOfStudents = numberOfStudents;
}
get name() {
return this._name;
}
get level() {
return this._level;
}
get numberOfStudents() {
return this._numberOfStudents;
}
set numberOfStudents(val) {
if(typeof val !== 'number') {
console.log('Invalid input: numberOfStudents must be set to a Number.')
} else {
this._numberOfStudents = val;
}
}
quickFacts() {
console.log(`${this.name} educates ${this.numberOfStudents} students at the ${this.level} school level.`)
}
static pickSubstituteTeacher(substituteTeachers) {
const randInd = Math.floor(Math.random() * substituteTeachers.length)
return substituteTeachers[randInd];
}
}
class PrimarySchool extends School {
constructor(name, numberOfStudents, pickupPolicy) {
super(name, 'primary', numberOfStudents)
this._pickupPolicy = pickupPolicy;
}
get pickupPolicy() {
return this._pickupPolicy;
}
}
class HighSchool extends School {
constructor(name, numberOfStudents, sportsTeams) {
super(name, 'high', numberOfStudents)
this._sportsTeams = sportsTeams;
}
get sportsTeams() {
return this._sportsTeams;
}
}
const lorraineHansburry = new PrimarySchool('Lorraine Hansbury', 'g', 'Students must be picked up by a parent, guardian, or a family member over the age of 13.');
lorraineHansburry.quickFacts();
const alSmith = new HighSchool('Al E. Smith', 415, ['Baseball', 'Basketball', 'Volleyball', 'Track and Field'])
alSmith.quickFacts();
这里是保存的 jsfiddle,展示了它的工作原理
我正在 CodeCademy 上做一个项目,我需要为一些学生创建一个 setter 方法。它应该检查该值是否为数字,如果是,则 return 该值,否则记录错误。我最初想出了这个代码
set numberOFStudents(val) {
if(typeof val === 'number') {
this._numberOfStudents = val;
} else {
console.log('Invalid input: numberOfStudents must be set to a Number.')
}
}
但它没有起作用,因为该值没有效果。它将 return 字符串和数字都作为值。 但是,当我在常规函数中使用此方法时,它工作得很好 https://jsfiddle.net/Montinyek/v8zsL5et/12/ 演练中使用的方法是这样的:
set numberOfStudents(val) {
if(val.isNaN()) {
console.log('Invalid input: numberOfStudents must be set to a Number.')
} else {
this._numberOfStudents = val;
}
}
这在常规函数中不起作用,因为它 return 是一个错误 https://jsfiddle.net/Montinyek/e6k7qdcs/ 但是更让我困惑的是,演练中使用的方法似乎也没有效果。我仍然可以将学生人数设置为一个字符串,它会记录它,而不是记录错误。这是整个代码的 jsfiddle https://jsfiddle.net/Montinyek/1j7wL5gm/
这是因为您没有在构造函数中使用 setter。因此,当您传入字符串时,它会分配给它。
你需要改变
constructor(name, level, numberOfStudents) {
this._name = name;
this._level = level;
this._numberOfStudents = numberOfStudents;
}
到
constructor(name, level, numberOfStudents) {
this._name = name;
this._level = level;
this.numberOfStudents = numberOfStudents;
}
所以它使用 setter 而不是成员变量。第一种方式直接将其分配给您的成员,而不是使用 setter.
这是根据您的示例进行更改的完整代码;
class School {
constructor(name, level, numberOfStudents) {
this._name = name;
this._level = level;
this.numberOfStudents = numberOfStudents;
}
get name() {
return this._name;
}
get level() {
return this._level;
}
get numberOfStudents() {
return this._numberOfStudents;
}
set numberOfStudents(val) {
if(typeof val !== 'number') {
console.log('Invalid input: numberOfStudents must be set to a Number.')
} else {
this._numberOfStudents = val;
}
}
quickFacts() {
console.log(`${this.name} educates ${this.numberOfStudents} students at the ${this.level} school level.`)
}
static pickSubstituteTeacher(substituteTeachers) {
const randInd = Math.floor(Math.random() * substituteTeachers.length)
return substituteTeachers[randInd];
}
}
class PrimarySchool extends School {
constructor(name, numberOfStudents, pickupPolicy) {
super(name, 'primary', numberOfStudents)
this._pickupPolicy = pickupPolicy;
}
get pickupPolicy() {
return this._pickupPolicy;
}
}
class HighSchool extends School {
constructor(name, numberOfStudents, sportsTeams) {
super(name, 'high', numberOfStudents)
this._sportsTeams = sportsTeams;
}
get sportsTeams() {
return this._sportsTeams;
}
}
const lorraineHansburry = new PrimarySchool('Lorraine Hansbury', 'g', 'Students must be picked up by a parent, guardian, or a family member over the age of 13.');
lorraineHansburry.quickFacts();
const alSmith = new HighSchool('Al E. Smith', 415, ['Baseball', 'Basketball', 'Volleyball', 'Track and Field'])
alSmith.quickFacts();
这里是保存的 jsfiddle,展示了它的工作原理