面向对象的js

js 代码
  1. /*  
  2.       * prototype is an attribute in Object of javascript,by attribute pototype one attribute and function can  
  3.       * contact with Object.by the way prototype attribute is working with key words 'new'  
  4.       * here is an example:  
  5.       */  
  6.      function MyObject(name,size){   
  7.       this.name = name;   
  8.       this.size = size;   
  9.      }   
  10.      MyObject.prototype.tellSize=function(){   
  11.       alert("size of "+this.name+" is "+this.size)   
  12.      }   
  13.      var MyObj = new MyObject("tiddles","7.5 meters")   
  14.      MyObj.tellSize();  

========================

js 代码
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">   
  2. <html>   
  3. <head>   
  4.     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />   
  5.     <title>Untitled Document</title>   
  6.     <script>   
  7.      var myobject = {   
  8.       objectId:"1",   
  9.       objectname:"michael",   
  10.        
  11.       getObjectName:function(){   
  12.        return this.objectname;   
  13.       },   
  14.        
  15.       setObjectName:function(objname){   
  16.        this.objectname = objname;   
  17.       },   
  18.        
  19.       action_shout:function(){   
  20.        alert("My name is "+this.objectname+"\nand my id is"+this.objectId);   
  21.       }   
  22.      }   
  23.     </script>   
  24. </head>   
  25. <body>   
  26.     <script>   
  27.      myobject.action_shout();   
  28.      myobject.setObjectName("hupeng");   
  29.      myobject.action_shout();   
  30.     </script>   
  31. </body>   
  32. </html>  

======================

js 代码
  1. 内部变量可以隐式的设置   
  2.   
  3.     function selectedObject(sid,sname){   
  4.      this.sid = sid;   
  5.      this.sname = sname;   
  6.     }   
  7.   
  8.     var myobject2 = {   
  9.      setValues:function(selectedObject){   
  10.       this.objid = selectedObject.sid;   
  11.       this.objname = selectedObject.sname;   
  12.      },   
  13.      action_shout:function(){   
  14.       alert(this.objid+"\n"+this.objname);   
  15.      }   
  16.     }   
  17.   
  18.    <script>   
  19.     var selectedobj = new selectedObject(6,"hupeng")   
  20.     myobject2.setValues(selectedobj)   
  21.     myobject2.action_shout()   
  22.    </script>   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值