<div class="codetitle"> <span><a style="CURSOR: pointer" data="22205" class="copybut" id="copybut22205" onclick="doCopy('code22205')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code22205"> <br><script type="text/javascript"> <BR><!-- <BR>/* <BR>给原型 prototype 添加属性的两种方式 <BR>*/ <BR>//方式一 <BR>var myObj = function(){ <BR>this.study = "JavaScript"; <BR>} <BR>myObj.prototype.hobby = function() <BR>{ <BR>this.hobby = "See girl"; <BR>} <BR>var newObj = new myObj(); <BR>for ( var attr in newObj ) <BR>{ <BR>document.write( attr +"<br/>" ); <BR>} <BR>document.write( "==================== <br/>" ); <BR>//方式二 <BR>var superObj = { name:"xugang" }; <BR>var subObj = { age:20 }; <BR>function extend(superObj,subObj){ <BR>//获得父对象的原型对象 <BR>subObj.getSuper = superObj.prototype; <BR>//将父对象的属性给子对象 <BR>for(var i in superObj){ <BR>subObj[i] = superObj[i]; <BR>} <BR>} <BR>extend(superObj,subObj); <BR>for ( var s in subObj ) <BR>{ <BR>document.write( s +"<br/>" ); //遍历子对象的属性 <BR>} <BR>//--> <BR></script> <br> </div> <br>运行结果: <br><br>hobby <br>study <br>==================== <br>age <br>getSuper <br>name