定义一个明星Star构造函数,通过此构造函数创建的对象包含姓名、年龄、身高等属性,并且包含一个方法可以在控制台输出这个明星的代表作。
通过Star构造函数,创建三个明星对象,并在控制台输出他们的属性以及代表作。
先了解一下js构造函数,然后根据想要的方法和属性写就行了,参考这个改一下属性和方法就行
//对象
let Student = {
number: "",
name: "",
age: "",
sex: "",
play: function () {
console.log('我最喜欢玩游戏')
}
}
console.log(Student);
Student.play();
//函数对象
function Student1(number, name, age, sex, game) {
this.number = number;
this.name = name;
this.age = age;
this.sex = sex;
this.play = function () {
console.log('我最喜欢玩' + game);
}
}
var my = new Student1(1, '威威', 15, '男', '王者');
console.log(my);
my.play();
//class类
class Parent {
constructor(number, name, age, sex, game) {
this.number = number;
this.name = name;
this.age = age;
this.sex = sex;
this.play = function () {
console.log('我最喜欢玩' + game);
}
}
};
const child = new Parent(1, '威威', 15, '男', '王者');
console.log(child);
child.play();