代码之家  ›  专栏  ›  技术社区  ›  phpdroid

父对象的值未从原型继承更改

  •  0
  • phpdroid  · 技术社区  · 8 年前

    从继承对象更改父对象的值学生应该更改人的年龄,但得到的值完全相同。

    <script>
    function Person(age){ 
        this.age=age;
    }
    
        function Student(){}
    
        var person=Student.prototype=new Person(10);
        var oldAge=person.age;
        Student.age=20;
        var newAge=person.age;
    
        alert("Person old age="+oldAge+"\New age="+newAge);
    </script>
    

    person Student 继承自相同 Person 那么学生和人的年龄价值都应该随着学生价值观的变化而变化

    我已经通过了 Prototypical inheritance - writing up JavaScript Inherited Properties Default Value 问题

    问题是我想通过继承人的财产的学生来改变人的价值。

    我想我遗漏了一些东西,请帮助我理解这一点。

    3 回复  |  直到 8 年前
        1
  •  3
  •   varunsinghal65    8 年前

    有两种模式用于在javascript中实现继承

    1. 原型面向对象模式
    2. 构造函数面向对象模式

    现在我将使用第一种方法

    一些必备知识:

    1. 所有JS对象都有一个指向原型的属性 对象,因此除了它自己的属性外,对象还可以 访问其自身原型的特性

    2. __proto__

    3. Object.create(arg) :用于创建对象和初始化 他们的原型或设置 __原型__ 所有物

      Object.create MDN link

      下面的代码段实现了继承,并允许您通过学生修改Person的值。 :

    function Person(age){ 
        this.age=age;
        this.getAge = function () { return this.age;}
    };
    
    function Student(){};
    
    //Creating Person instance
    var person = new Person(23);
    
    console.log("Old Person age is " + person.age);
    
    //Creating a student instance and inheriting it from person instance
    //Object.create method creates a object whose __proto__ point to the object passed
    //Thus student will be an object having a property __proto__ that would point to person instance
    //This assosciation allows the instance of student to access insatnce of Person
    var student = Object.create(person);
    
    //Change age of person through student
    student.__proto__.age = 24;
    
    console.log("New Person age is " + person.age);
    
    console.log("We can also call parent object methods from child" + " for e.g calling getAge from student" + student.getAge());

    现在,为了使用第二种方法实现类似的效果,可以使用以下代码段:

    function Person(age){ 
        this.age=age;
    }
    
    function Student(){}
    
    //Create person instance
    var person = new Person(23);
    
    console.log("Old age of person is " + person.age);
    
    //Inherit the person instance
    Student.prototype = person;
    
    //create a student object
    var student = new Student();
    
    //Change the person instance age value
    //this change is possible because we
    //can access person object through
    //student.__proto__.
    student.__proto__.age = 24;
    
    console.log("New age of person is " + person.age);
        2
  •  0
  •   Fenton    8 年前

    person.age Student.age 是与实例无关的静态属性。

    您可以简化整个示例以删除student,但仍然可以看到类似实例属性和静态属性的内容。

    function Person(age){ 
        this.age = age;
    }
    
    var person = new Person(10);
    var oldAge = person.age;
    Person.age = 20;
    var newAge = person.age;
    
    alert("Person old age=" + oldAge + "\New age=" + newAge);
    alert(Person.age);
    
        3
  •  0
  •   Jonathan Wang    8 年前

    在JavaScript中,您应该始终使用 原型继承 让这一切顺利进行。

    var person = { 
        age: 10
    }
    var student = Object.create(person);
    var oldAge=person.age;
    student.age=20;
    var newAge=student.age;
    
    alert("Person old age="+oldAge+"\New age="+newAge);
    

    在您的代码中,由于function Student在 ,JavaScript引擎将在内存中生成一个名为age的属性。在执行阶段,JavaScript引擎将为在创建阶段创建的新属性分配20。

    如果在浏览器内执行,您会注意到函数Student有一个新属性,称为age,等于20。