我正在尝试创建一个 JS 对象构造函数,但想使用布尔运算声明对象的属性之一。不知何故我弄错了 - 请指教!
谢谢!交流电
function Student(gender, name, id, age, likes) {
this.gender = gender;
this.name = name;
this.id = id;
this.age = Boolean () {
var age = x;
if (age >= 21) {
return "adult";
} else {
return "minor";
}
};
this.likes = likes;
}
window.onload = init;
function init() {
var jessica = new Student("female", "Jessica", 12, 21, ["butterflies", "flowers", "music"]);
var jessicaJSON = JSON.stringify(jessica);
console.log(jessicaJSON);
var anotherJessica = JSON.parse(jessicaJSON);
console.log(anotherJessica);
}