Javascriptにおける対象継承の実現例

2228 ワード

 
  




Insert title here
<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>