本节作业之创建电脑对象、按钮对象、车的对象、一个简易计算器
1 对象练习题
创建一个电脑对象,该对象要有颜色、重量、品牌、型号,可以看电影、听音乐、打游戏和敲代码
function Computer(color,weight,brand,type) {
this.color = color;
this.weight = weight;
this.brand = brand;
this.type = type;
this.see = function(movie) {
console.log(movie);
}
this.listen = function(song) {
console.log(song);
}
this.play = function(game) {
console.log(game);
}
this.study = function(code) {
console.log(code);
}
}
var huawei = new Computer('灰','100g','huawei','matebook14');
console.log(huawei.color);
console.log(huawei.weight);
console.log(huawei.brand);
console.log(huawei.type);
huawei.see('我不是药神');
huawei.listen('稻香');
huawei.play('双人成行');
huawei.study('Hello Word');
![在这里插入图片描述](https://img-blog.csdnimg.cn/edef70b5f94a4c3ea180adbaf7c05319.png)
创建一个按钮对象,该对象中需要包含宽、高、背景颜色和点击行为
function Button(width,height,bgcolor) {
this.width = width;
this.height = height;
this.bgcolor = bgcolor;
this.click = function(behavior) {
console.log('点击行为');
}
}
var bt1 = new Button('50px','30px','purple');
console.log(bt1['width']);
console.log(bt1['height']);
console.log(bt1['bgcolor']);
bt1.click();
![在这里插入图片描述](https://img-blog.csdnimg.cn/56ff5da228f64724be23838984cb0178.png)
创建一个车的对象,该对象要有重量、颜色、牌子,可以载人、拉货和耕田
var car = {
weight: '1吨',
color: 'pink',
brand: '奥迪',
gongneng: function() {
console.log('载人 拉货 耕田');
}
}
for(var k in car) {
console.log(k);
console.log(car[k]);
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/b466c4c44259415ebd204ea58ffb98eb.png)
2 一个简易计算器
function input1() {
var num1 = prompt('请输入第一个数:');
return parseFloat(num1);
}
function input2() {
var num2 = prompt('请输入第二个数:');
return parseFloat(num2);
}
function add(num1, num2){
return num1 + num2;
}
function sub(num1, num2){
return num1 - num2;
}
function multiply(num1, num2){
return num1 * num2;
}
function divition(num1, num2){
return num1 / num2;
}
var result;
do {
var option = parseInt(prompt('欢迎使用简易计算器:\n1. 加法运算;\n2. 减法运算;\n3. 乘法运算;\n4. 除法运算;\n5. 退出:\n请输入您的选项'));
switch (option) {
case 1:
result = add(input1(), input2());
alert('结果是 ' + result);
break;
case 2:
result = sub(input1(), input2());
alert('结果是 ' + result);
break;
case 3:
result = multiply(input1(), input2());
alert('结果是 ' + result);
break;
case 4:
result = divition(input1(), input2());
alert('结果是 ' + result);
break;
case 5:
alert('已退出程序');
break;
default:
alert('输入错误,请重新输入');
break;
}
} while (option != 5);