Home  >  Q&A  >  body text

javascript - JS inheritance, how to avoid the situation where the object type attribute of the parent class prototype will be affected by the subclass;

parent is the parent class, child inherits parent, when the value of sex of the child instance is modified When parent and other subclass instancessex will be changed; In the constructor
, or it is agreed that the object attribute in the prototype is not allowed to be modified to avoid the impact of subclass instances on the parent class and other subclasses; Are there any other solutions? plan?

    function extend(p, c) {
        var f = function () {};
        f.prototype = p.prototype;
        c.prototype = new f();
        c.prototype.constructor = c;
    }
    function parent() {}
    parent.prototype.aaa = 123;
    parent.prototype.sex = ['男', '女'];

    function child() {}
    extend(parent, child);
    c1 = new child();
    c2 = new child();
    console.log('设置实例c1之前,父类的sex值:',parent.prototype.sex);
    console.log('设置实例c1之前,实例c2的sex值:',c2.sex);
    c1.sex.push('其他');
    console.log('设置实例c1之后,父类的sex值:',parent.prototype.sex);
    console.log('设置实例c1之后,实例c2的sex值:',c2.sex);

过去多啦不再A梦过去多啦不再A梦2691 days ago876

reply all(4)I'll reply

  • 扔个三星炸死你

    扔个三星炸死你2017-06-30 10:01:00

    This method allows subclasses and objects to access sex. If sex does not exist, a copy of the parent class sex will be created for it. If it exists, it will be returned directly.

    function extend(p, c) {
        var f = function() {};
        f.prototype = p.prototype;
        c.prototype = new f();
        c.prototype.constructor = c;
    }
    
    function parent() {}
    parent.sex = ['男', '女'];
    parent.prototype.aaa = 123;
    Object.defineProperty(parent.prototype, 'sex', {
      configurable: true,
      enumerable: true,
      get: function () {
        if (this === parent || this === parent.prototype) {
          return parent.sex;
        }
    
        if (!this.hasOwnProperty('sex')) {
          Object.defineProperty(this, 'sex', {
            value: parent.sex.slice(),
            configurable: true,
            enumerable: true,
            writable: true
          });
        }
        return this.sex
      },
      set: function (value) {
        if (this === parent || this === parent.prototype) {
          parent.sex = value;
        } else if (!this.hasOwnProperty('sex')) {
          Object.defineProperty(this, 'sex', {
            value: value,
            configurable: true,
            enumerable: true,
            writable: true
          });
        } else {
          this.sex = value;
        }
      }
    });
    
    function child() {}
    extend(parent, child);
    var c1 = new child();
    var c2 = new child();
    var p1 = new parent();
    console.log('设置实例c1之前,父类的sex值:', parent.prototype.sex);
    console.log('设置实例c1之前,实例p1的sex值:', p1.sex);
    console.log('设置实例c1之前,实例c2的sex值:', c2.sex);
    c1.sex.push('其他');
    console.log('设置实例c1之后,父类的sex值:', parent.prototype.sex);
    console.log('设置实例c1之后,实例p1的sex值:', p1.sex);
    console.log('设置实例c1之后,实例c1的sex值:', c1.sex);
    console.log('设置实例c1之后,实例c2的sex值:', c2.sex);

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-06-30 10:01:00

    The subclass defines an attribute with the same name, overriding the parent class’s?

    reply
    0
  • 三叔

    三叔2017-06-30 10:01:00

    Non-method attributes are not recommended to be set on the prototype

    reply
    0
  • 学习ing

    学习ing2017-06-30 10:01:00

    When initializing the subclass child, define private properties:

    function child() {
        this.sex = '';
    }

    reply
    0
  • Cancelreply