<div class="codetitle"> <span><a style="CURSOR: pointer" data="22857" class="copybut" id="copybut22857" onclick="doCopy('code22857')"><u>複製程式碼</u></a></span> 代碼如下:</div> <div class="codebody" id="code22857"> <br> <br><br> <br> <br> <br><meta charset="UTF-8"> <br><title>Insert title here</title> <br><script type="text/javascript"> <BR>/**<BR>* json物件的格式 <BR>{key:value,key:value,key:value..} <BR> */ <BR>//建立物件的小範例<BR>//-----1 <BR>var r={}; <BR>r.name="tom"; <BR>r.age= 18; <BR>//-----2 <BR>var r={name:"tom",age:20};//json物件<BR>alert(r.age); <BR>// ---1,2是等價的<BR>//-------原型模式的寫法<BR>//----1 <BR>function Person(){}; <BR>Person .prototype.name="中國人"; <BR>Person.prototype.age=20; <BR>//原型模式的簡寫形式--2 <BR>function Person(){}; <BR>Person.prototype ={name:"中國人", <BR>age:20,} <BR>//-----1,2等價的<BR>//============ ==================== <BR>/* {name:"中國人", <BR>age:20,} <BR>上面的這種格式本身就是個對象,將其付給另一個對象的prototype,就使得<BR>另一個對象的所有屬性。實質上就是繼承<BR>*/ <BR>//================================ <BR>/ /標準的物件繼承例子,Person,Student <BR>//定義一個Person物件<BR>function Person(){}; <BR>Person.prototype.name="中國人"; <BR>Person.prototype. age=20; <BR>var person=new Person(); <BR>//定義一個Student物件<BR>function Student(){}; <BR>Student.prototype=person; <BR>Student.prototype. girlFriend="可以有的"; <BR>var stu=new Student(); <BR>stu.laop="不准談戀愛"; <br>alert(stu.name);//繼承自父物件的實例<br>alert(stu.laop);//自己新加入的屬性<BR><BR>//定義一個Teamleader物件的<BR>function Teamleader(){}; <BR>Teamleader.prototype=new Student( );//繼承自Student <BR>Teamleader.prototype.teamNum=8;//Teamleader自己的屬性<BR>//建立自己的實例<BR>var teamleader=new Teamleader(); <BR>alert(teamleader .teamNum); <BR>teamleader.girlFriend="也不可以有哦"; <BR>alert(teamleader.name); <BR>//================ ================= <BR>/*js中繼承的核心就是prototype*/ <BR>//============== =================== <BR></script> <br> <br> <br><br> </div>