在js中可以用对象冒充来实现继承机制,我做了个例子如下所示:function people(name,age){
this.name=name;
this.age=age;
this.sayHello=function(){
alert("Hello,"+this.name)
}
}
function student(name,age,grade,school){
this.people=people;
this.people(name,age)
this.grade=grade;
this.school=school;
}
var s=new student("小伟",25,"classI","中国大学");
s.sayHello()这样可以弹出"Hello,小伟",我修改了代码,如下所示:function people(name,age){
this.name=name;
this.age=age;
this.sayHello=function(){
alert("Hello,"+this.name)
}
}
function student(name,age,grade,school){
//this.people=people;
//this.people(name,age)
this.people=function(name,age){
this.name=name;
this.age=age;
this.sayHello=function(){
alert("Hello,"+this.name)
}
}
this.grade=grade;
this.school=school;
}
var s=new student("小伟",25,"classI","中国大学");
//s.people("然思维",25)
s.sayHello()这时再执行就不弹出了,直接报错,说s.sayHello()不是一个function,我想问下这是为什么?区别在哪里?

解决方案 »

  1.   

    你这样写 让我很纠结
    s.people.sayhello();这样试下
      

  2.   

    student 没有继承 people 
      

  3.   


    function people(name, age) {
        this.name = name;
        this.age = age;
    }
    people.prototype.sayHello = function() {
        alert("Hello," + this.name)
    }
    function student(name, age, grade, school) {
        this.name = name;
        this.age = age;
        this.grade = grade;
        this.school = school;
    }student.prototype = new people();
    student.prototype.getSchool = function() {
        alert(this.school);
    };
    var s = new student("小伟", 25, "classI", "中国大学");
    s.sayHello();
    s.getSchool();
      

  4.   

    楼上的通过原型对象方法继承是可以的,这里提供另外一种继承方法 function people(name,age){ 
    this.name=name; 
    this.age=age; 
    this.sayHello=function(){ 
    alert("Hello,"+this.name) 


    function student(name,age,grade,school){ 
                    // 利用函数的call()或者apply()方法也可以实现继承
    people.call(this,name,age);
    this.grade=grade; 
    this.school=school; 

    var s=new student("小伟",25,"classI","中国大学"); 
    s.sayHello()
      

  5.   

    我的理解是:
    this.people=people;
    this.people(name,age)
    这样调用时,people定义里的this.sayHello里的this是指student对象this.people=function(name,age){
         this.name=name;
         this.age=age;
         this.sayHello=function(){
             alert("Hello,"+this.name)
         }
     }
    这样调用时,this.sayHello里的this指的是this.people,所以应该是s.people.sayHello()感觉挺绕的……
      

  6.   

    我是5楼的
    又想了一下,我在楼上说的不正确
    实际上第二种方法不能用是在因为对象声明this.people只是赋了值,但是未被执行过,即是说在调用student.people之前,this.sayHello=function()...实际未执行
    所以应该先执行s.people方法,再调用sayHello函数,即
    s.people(s.name, s.age);
    s.sayHello();
      

  7.   

    function student(name,age,grade,school){
        //this.people=people;
        //this.people(name,age)
        this.people=function(name,age){
            this.name=name;
            this.age=age;
            this.sayHello=function(){
                alert("Hello,"+this.name)
            }
        }

        this.people();
        this.grade=grade;
        this.school=school;
    }
    上面那段FUNCTION还没有没执行 只是被赋予了people
    后面加句 this.people();应该就可以了
      

  8.   


    你这样写当然不能这样调用了,应该是s.people.sayHello();
    当然你样就不是继承了,相当于重写
      

  9.   

    不知道楼主到底想怎样,其实用是可以用的啊。只是那个注释掉的应该取消注释。因为你的SayHello是在people函数里定义的,必须执行它才能有你这个SayHello。
    <script type="text/javascript">function student(name,age,grade,school){
        //this.people=people;
        //this.people(name,age)
        this.people=function(name,age){
            this.name=name;
            this.age=age;
            this.sayHello=function(){
                alert("Hello,"+this.name)
            }
        }
        this.grade=grade;
        this.school=school;
    }
    var s=new student("小伟",25,"classI","中国大学");
    s.people("然思维",25);
    s.sayHello();</script>
      

  10.   

    或者这样:
    <script type="text/javascript">
    function student(name,age,grade,school){
        this.people=function(name,age){
            this.name=name;
            this.age=age;
            this.sayHello=function(){
                alert("Hello,"+this.name)
            }
        }
        this.people(name,age);
        this.grade=grade;
        this.school=school;
    }
    var s=new student("小伟",25,"classI","中国大学");
    s.sayHello();</script>
      

  11.   

    这样写法其实和下面这种写法是一样的:function people(name,age){
        this.name=name;
        this.age=age;
        this.sayHello=function(){
            alert("Hello,"+this.name)
        }
    }
    function student(name,age,grade,school){
        this.people=people;
        this.people(name,age)

        this.grade=grade;
        this.school=school;
    }
    var s=new student("小伟",25,"classI","中国大学");
    s.sayHello()this.pepole只是赋值但是没有执行,对象冒充其实和用call来继承一样,上面的写法等价于:function people(name,age){
        this.name=name;
        this.age=age;
        this.sayHello=function(){
            alert("Hello,"+this.name)
        }
    }
    function student(name,age,grade,school){
      people.call(this,name,age);
        this.grade=grade;
        this.school=school;
    }
    var s=new student("小伟",25,"classI","中国大学");
    s.sayHello()
      

  12.   

    对象冒充的精髓在于this对象的替换
    而javascript是动态执行的。所以写在闭包内的this并不认识外面的function
    必须要执行下。才能把里面的this,动态链接到外面的this上。从而实现继承。
    p.s.
    我认为你的第一种对象冒充写法还是不完全
    function people(name,age){
        this.name=name;
        this.age=age;
        this.sayHello=function(){
            alert("Hello,"+this.name)
        }
    }
    function student(name,age,grade,school){
        this.people=people;
        this.people(name,age);
        delete this.people;
        this.grade=grade;
        this.school=school;
    }
    var s=new student("小伟",25,"classI","中国大学");
    s.sayHello();
    这句话很重要,否则你的子类中会多一个this.people属性