`
zeng7960983
  • 浏览: 42927 次
  • 性别: Icon_minigender_1
  • 来自: 邵阳
社区版块
存档分类
最新评论

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>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics