js 面向对象学习2

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js面向对象学习2</title>
</head>

<body>
<script>
//现实生活中电脑一般有主机(host),键盘(keyboard),鼠标(mouse),显示器(monitor)
//主机可能包含有主板(motherboard),CPU,硬盘(HardDisk),声卡(AudioCard),显卡(GraphicCard)
//显示器(monitor):价格(price),厂家(factory),型号(type),尺寸(size)
//首先创建一个显示器对象
var monitor={ //使用对象直接量创建一个monitor对象
price:1800,
factory:"xx00公司",
type:"呵呵",
size:17,
close:function(){ //第二中方法给对象创建方法
document.write("我关了显示器<br>");
},
modulate:new Function('document.write("我调整显示器");') //第三中方法
};
document.write("显示器的厂家:"+monitor.factory+"<br />");
document.write("显示器的价格:"+monitor.price+"<br />");
document.write("显示器的类型:"+monitor.type+"<br />");
document.write("显示器的大小:"+monitor.size+"<br />");
//现在我们为显示器创建一个打开方法
monitor.open=function(){
//return "价值"+this.price+"的"+this.factory+"";
document.write("价值"+this.price+"的"+this.factory+"<br>");
}
//alert(monitor.open());
monitor.open();
monitor.close();
monitor.modulate();

//下面使用对象构造函数创建对象
function Monitor(_price,_factory,_type,_size){
this.price=_price,
this.factory=_factory,
this.type=_type,
if(_size==undefined){
this.size=15;
}
else{
this.size=_size
}
}

var monitor1=new Monitor(1800,"haha","hehe");
var monitor2=new Monitor(2000,"a","b",18);

document.write("<br>第一台显示器的大小"+monitor1.factory+"<br>");
document.write("<br>第一台显示器的厂家"+monitor1.factory+"<br>");

document.write("<br>第一台显示器的厂家"+monitor1.factory+"<br>");
document.write("第二台显示器的厂家"+monitor2.factory);
</script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值