创建一个歌星构造函数,有姓名,性别,年龄,可以唱成名曲,实例化三个明星js
时间: 2024-10-27 09:14:26 浏览: 20
创建一个JavaScript歌星构造函数,我们可以设计它来包含姓名、性别、年龄以及一个唱歌列表(这里我们暂且称其为“成名歌曲”)。下面是一个简单的示例:
```javascript
// 定义歌星构造函数
function Singer(name, gender, age, hitSongs) {
this.name = name;
this.gender = gender;
this.age = age;
this.hitSongs = hitSongs; // 成名歌曲数组
}
// 实例化三位明星
let star1 = new Singer("张三", "男", 30, ["爱情故事", "青春修炼手册"]);
let star2 = new Singer("李四", "女", 25, ["小幸运", "匆匆那年"]);
let star3 = new Singer("王五", "男", 40, ["岁月神偷", "遥远的她"]);
// 显示每位明星的信息
console.log(star1);
console.log("明星1信息:", "姓名:", star1.name, ", 性别:", star1.gender, ", 年龄:", star1.age, ", 成名歌曲:", star1.hitSongs);
console.log(star2);
console.log("明星2信息:", "姓名:", star2.name, ", 性别:", star2.gender, ", 年龄:", star2.age, ", 成名歌曲:", star2.hitSongs);
console.log(star3);
console.log("明星3信息:", "姓名:", star3.name, ", 性别:", star3.gender, ", 年龄:", star3.age, ", 成名歌曲:", star3.hitSongs);
阅读全文