面向对象(二继承)

function CreatePerson(name,sex){//父类
                this.name=name;
                this.sex=sex;
            }
            CreatePerson.prototype.showName=function(){
                alert(this.name);
            }
            
            var p1=new CreatePerson("xiaomi","boy");
            p1.showName();
            
            function CreateStar(name,sex,job){//子类
                //对属性的继承  因为CreatePerson前面没有变量所以他的this指向会是window
                //这里要call把this指向指到p2上
                CreatePerson.call(this,name,sex);
                this.job=job;
            }
            CreateStar.prototype.showJob=function(){
                //方法的继承
                for(var attr in CreatePerson.prototype){
                    CreateStar.prototype[attr]=CreatePerson.prototype[attr];
                }
            }
            
            var p2=new CreateStar("xiaomi","boy","worker");
            p2.showName();

 

posted @ 2015-08-04 16:27  Mi文  阅读(111)  评论(0编辑  收藏  举报