本节作业之创建电脑对象、按钮对象、车的对象、一个简易计算器

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');

在这里插入图片描述

创建一个按钮对象,该对象中需要包含宽、高、背景颜色和点击行为

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();

在这里插入图片描述

创建一个车的对象,该对象要有重量、颜色、牌子,可以载人、拉货和耕田

var car = {
	weight: '1吨',
	color: 'pink',
	brand: '奥迪',
	gongneng: function() {
		console.log('载人 拉货 耕田');
	}
}
for(var k in car) {
	console.log(k);
	console.log(car[k]);
}

在这里插入图片描述

2 一个简易计算器

function input1() {
	var num1 = prompt('请输入第一个数:');
    return parseFloat(num1);
}
function input2() {
    var num2 = prompt('请输入第二个数:');
    return parseFloat(num2);
}
// 1. 加法运算
function add(num1, num2){
	return num1 + num2;
}
// 2. 减法运算
function sub(num1, num2){
    return num1 - num2;
}
// 3. 乘法运算
function multiply(num1, num2){
    return num1 * num2;
}
// 4. 除法运算
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);
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小王小王几点了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值