【问题标题】:How to change value for a variable in Javascript prototype如何在 Javascript 原型中更改变量的值
【发布时间】:2020-09-24 22:43:15
【问题描述】:

请检查下面的代码,让我知道我错过了什么。

function Student() {
  this.name = "John";
  this.type = "Regular";
}

Student.prototype.changeType = function(givenType) {
  this.type = givenType;
}

Student.prototype.changeType('Part-Time');

var William = new Student();

console.log(William.type); //Regular

控制台期望是Part-Time

【问题讨论】:

    标签: javascript function-prototypes


    【解决方案1】:

    编辑:如果您想为所有当前和未来的实例设置类型,您可以尝试存储所有实例。您必须在添加所有新学生时更新他们。 JavaScript 不知道如何仅通过类名来获取当前和未来的所有对象。

    function Student() {
      this.name = "John";
      this.type = "Regular";
      Student.instances.push(this); // Add
    }
    Student.prototype.changeType = function(givenType) {
      this.type = givenType;
    }
    
    Student.instances = []; // Keep track
    Student.changeAllTypes = function(type) {
      Student.instances.forEach(function(instance) {
        instance.changeType(type);
      });
    };
    
    var William = new Student();
    
    Student.changeAllTypes('Part-Time');
    
    console.log(William.type); // Part-Time

    正确的做法是在对象实例化期间传入适当的参数。

    function Student(name, type) {
      this.name = name;
      this.type = type;
    }
    
    var william = new Student('William', 'Part-Time');
    
    console.log(william.type); // Part-Time

    原型与实例不同。

    function Student() {
      this.name = "John";
      this.type = "Regular";
    }
    
    Student.prototype.changeType = function(givenType) {
      this.type = givenType;
    }
    
    Student.prototype.changeType('Part-Time');
    
    var William = new Student();
    
    console.log(Student.prototype.type); // Part-Time

    如果你想改变类型,你需要将属性设置为只是威廉。

    function Student () {
        this.name = "John";
        this.type = "Regular";
    }
    
    Student.prototype.changeType = function (givenType) {
       this.type = givenType;
    }
    
    var William = new Student();
    
    William.changeType('Part-Time');
    
    console.log(William.type); // Part-Time

    或者你可以创建一个原生的 ES6 类,这样会更容易。

    class Student {
      constructor(name = 'John', type = 'Regular') {
        this.name = name;
        this.type = type;
      }
      setType(type) {
        this.type = type;
      }
      getType() {
        return this.type;
      }
    }
    
    var william = new Student();
    
    william.setType('Part-Time');
    
    console.log(william.getType()); // Part-Time

    【讨论】:

    • 感谢您的快速响应,实际上我正在尝试覆盖对象或道具中的现有方法。你能告诉我如何做到这一点吗?
    • @rkaartikeyan 所以,反思?
    【解决方案2】:

    JS 仅在对象本身没有设置属性值的情况下检查原型链的下一级。

    您是在原型上调用changeType,而不是在对象本身上。

    构造函数设置对象本身的属性。

    调用William.changeType("Part-Time") 会产生预期的效果。

    【讨论】:

    • 感谢您的快速响应,实际上我正在尝试覆盖对象或道具中的现有方法。您能否建议如何实现这一目标?
    • 如果要覆盖该方法,则需要为 changeType 属性分配一个新函数
    • 我在下面尝试过,但仍然无法正常工作。 ```函数学生(){ this.name =“约翰”; this.type = "常规"; this.changeType = function () { console.log('Part-Time'); } } Student.prototype.changeType= function (givenType) { this.type = givenType; } var William = new Student(); William.changeType('兼职');控制台.log(威廉.type); ```
    • 你期待什么?您覆盖了 changeType 方法,因此它调用了 console.log 而不是更改类型!
    • @Quentine,没错。我想改变现有的功能。
    【解决方案3】:

    我认为您需要一个结构,该结构具有引用实例 (this) constructorprototype 的方法。然后下一个new 实例将具有该属性,由先前调用的实例方法调用。

    function Student(name = null, type = null){
      this.changeName = name=>{
        this.constructor.prototype.name = name;
        return this;
      }
      this.changeType = type=>{
        this.constructor.prototype.type = type;
        return this;
      }
      if(name !== null)this.changeName(name);
      if(type !== null)this.changeType(type);
    }
    const john = new Student('John', 'Part-Time');
    console.log(john.name+' -> '+john.type);
    const bob = new Student('Bob');
    console.log(bob.name+' -> '+bob.type);
    const joe = new Student('Joe', 'Full-Time');
    console.log(joe.name+' -> '+joe.type);
    const dan = new Student('Dan');
    console.log(dan.name+' -> '+dan.type);

    【讨论】:

      猜你喜欢
      • 2015-09-13
      • 1970-01-01
      • 2018-04-24
      • 2011-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-15
      相关资源
      最近更新 更多