您的位置:首页 > 编程语言

体会面向对象过程与面向对象的编程思想

2018-11-28 12:14 357 查看

体会面向对象过程与面向对象的编程思想
加粗样式

Document
<script>
// 初级面向对象思想
document.getElementById('btn').onclick=function(){
document.getElementById("dv").style.backgroundColor="yellow";
};

// 高级面向对象思想
// 按钮是一个对象,div是一个对象,颜色是属性
//构造函数
function ChangeColor(btnId,dvId,color){
this.btnObj=document.getElementById(btnId);//按钮对象
this.dvObj=document.getElementById(dvId);//div对象
this.color=color;
}
//数据共享方式来改变背景颜色
ChangeColor.prototype.init=function(){
console.log(this);//this就是当前对象那个-----就是实例对象--就是S1
var that = this;
this.btnObj.onclick=function(){
that.dvObj.style.backgroundColor=that.color;
}
};
//实例化对象
var s1 = new ChangeColor("btn","dv","yellow");
s1.init();
</script>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: