javascript継承実現

805 ワード

<script type="text/javascript">
         //js     
         function Person() {//           
             this.name;
             this.age;
             this.say = function () {
                 alert('my name is:' + this.name);
             }
             this.showAge = function () {
                 alert('my age is:' + this.age);
             }
         }

         function Stu() {//      
             Person.call(this);//               
             this.showSchool = function () {//         
                 alert('my school is:'+this.school);
             }
         }

         var s2 = new Stu();
         s2.age = 18;
         s2.showAge();
         s2.school = "BJ";
         s2.showSchool();
</script>