js 面对对象和继承

       window.personCreate={};
            personCreate.Person=Person;
            personCreate.Student=Student;

            // person对象的构造函数
            function Person(name,age){
                this.name=name;
                this.age=age;

                if (typeof this.getName!='function') {
                    Person.prototype.saiName=function(){
                        alert(this.name);
                    };
                    Person.prototype.saiAge=function(){
                        alert(this.age);
                    };
                }
            }


            //student对象的构造函数
            function Student(name,age,school){
                // 继承属性(借用person的构造函数)
                Person.call(this,name,age);
                // 扩展属性
                this.school=school;

                
            }

            // 继承原型
            function inheritPrototype(subType,supperType){
                var pro=Object.create(supperType.prototype);
                pro.constructor=subType;
                subType.prototype=pro;
            }
            inheritPrototype(Student,Person);

            // 扩展方法
            Student.prototype.saiSchool=function(){
                alert(this.school);
            }

 

posted @ 2017-12-19 17:01  你怪哉  阅读(64)  评论(0)    收藏  举报