javascript - JS inheritance, how to avoid the situation where the object type attribute of the parent class prototype will be affected by the subclass;
过去多啦不再A梦
过去多啦不再A梦 2017-06-30 09:59:00
0
4
893

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梦

reply all(4)
扔个三星炸死你

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);
伊谢尔伦

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

三叔

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

学习ing

When initializing the subclass child, define private properties:

function child() {
    this.sex = '';
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template