javascript面向对象(一)

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8"/>
		<title>自我介绍</title>
		<link rel="Stylesheet" href="index.css"/>
	</head>
	<body>
		<div id="parentDiv">
<div id="lilei" οnmοuseοver="lilei.intrSelf()" οnmοuseοut="dialog.style.display='none'">
</div>
<div id="hmm" οnmοuseοver="hmm.intrSelf()" οnmοuseοut="dialog.style.display='none'">
</div>
			<div id="dialog">
			</div>
		</div>
		<script>
      function Student(name,age){
        this.name=name;
        this.age=age;
      }
      //向Student的原型对象中添加intrSelf方法
      Student.prototype.intrSelf=
        function(){
          dialog.innerHTML=
            "I'm "+this.name
            +",I'm "+this.age;
          dialog.style.display="block";
        }
      var lilei=
        new Student("Li Lei",11);
      var hmm=
        new Student("Han Meimei",12);
      console.log(
        Student.prototype//老公
          ==lilei.__proto__//爹
      );//true

Student.prototype.className="初一2班";
console.log(lilei.className);
console.log(hmm.className);
//Student.prototype.className="初二2班
lilei.className="初二2班";
//不报错:给lilei添加一个className属性
console.log(lilei.className);
console.log(hmm.className);
delete lilei.className;
console.log(lilei.className);
console.log(hmm.className);
//delete lilei.className;//没效果
delete Student.prototype.className;
console.log(lilei.className);
console.log(hmm.className);

    /*
			var lilei={
        "name":"Li Lei",
        "age":11,
        "intrSelf":function(){
          //this:lilei
          //设置id为dialog的元素的内容为:"I'm Li Lei,I'm 11";
          dialog.innerHTML=
            "I'm "+this.name
            +",I'm "+this.age;
          //设置id为dialog的元素的样式的display为block
          dialog.style.display="block";
        }
      }
     
      lilei.age++;//12
    */
      //定义hmm:姓名Han Meimei,年龄12
        //定义intrSelf方法
    /*
      var hmm={
        "name":"Han Meimei",
        "age":12,
        "intrSelf":function(){
          //this:hmm
          dialog.innerHTML=
            "I'm "+this.name
            +",I'm "+this.age;
          dialog.style.display="block";
        }
      }
    */
    /*
      //var hmm=new Object();//{}
      var hmm={};
      //hmm.name="Han Meimei";
      hmm["name"]="Han Meimei";
      hmm["age"]=12;
      hmm["intrSelf"]=function(){
        //this:hmm
        dialog.innerHTML=
          "I'm "+this.name
          +",I'm "+this.age;
        dialog.style.display="block";
      }
      console.dir(lilei);
      console.dir(hmm);
    */
    /*
      //克隆一个lilei
      function clone(obj){//浅克隆
        //创建新空对象copy
        var copy={};
        //遍历obj的属性
        for(var key in obj){
          //向copy中添加新的属性:
            //属性名为key,值为obj中key属性的值
          copy[key]=obj[key]
        }
        return copy;
      }
      var lilei_copy=clone(lilei);
      console.dir(lilei_copy);
      console.dir(lilei);
    */
		</script>
	</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值