定义一个手机对象myPhone,包含三个属性:颜色、品牌、尺寸(属性值自行定义),并且包含一个方法chat,这个方法可以在控制台输出一句聊天信息。
定义好myPhone对象后,在控制台打印输出此对象的所有属性,最后调用对象中的方法。
<script type="text/javascript">
class myPhone {
constructor(color, brand, size) {
this.color = color
this.brand = brand
this.size = size
}
chat(){
console.log('hello world')
}
}
let phone1 = new myPhone('红色', '苹果', 30);
console.log(phone1.color)
console.log(phone1.brand)
console.log(phone1.size)
phone1.chat()
</script>
结果:
参考一下这个,只需要修改属性和方法
//对象
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();