javascript creates objects object inheritance details of the practical way

  • 2021-01-25 07:08:00
  • OfStack

This article stipulates that properties refer to properties or methods without special declaration.

Object creation and object inheritance are really the same thing: we need instance objects to get private properties through the constructor and shared properties through the prototype chain. What is a good way? Private properties are acquired through constructors (not considering custom private properties in the instance) and do not need to be overridden. Shared properties are found through the prototype chain and do not need to be created repeatedly.

A universal approach

Objects are created using a combination of the constructor pattern and the stereotype pattern


function HNU_student(name) {
  this.name = name;
  this.sayName = function() {
    return this.name;
  };
}
HNU_student.prototype = {
  school: 'HNU',
  saySchool: function() {
    return this.school;
  }
};
Object.defineProperty(HNU_student, 'constructor', {value: HNU_student});

var hiyohoo = new HNU_student('xujian');

prototype is overwritten literally, and the prototype constructor refers to Object, and constructor needs to be redefined if necessary.

Parasitic combinatorial inheritance


function object(o) {
  function F() {};
  F.prototype = o;
  return new F();
}
function inheritPrototype(child, parent) {
  var prototype = object(parent.prototype);
  prototype.constructor = child;
  child.prototype = prototype;
}

function HNU_student(name) {
  this.name = name;
  this.sayName = function() {
    return this.name;
  };
}
HNU_student.prototype.school = 'HNU';
HNU_student.prototype.saySchool = function() {
  return this.school;
};

function Student_2011(name, number) {
  HNU_student.call(this, name);
  this.number = number;
  this.sayNumber = function() {
    return this.number;
  }
}
inheritPrototype(Student_2011, HNU_student);
Student_2011.prototype.graduationTime = 2015;
Student_2011.prototype.sayGraduationTime = function() {
  return this.graduationTime;
};

var hiyohoo = new Student_2011('xujian', 20110803203);

What object() does: It turns an object passed in as a parameter into a prototype of the instance, whose properties are shared by all instances.

Shared attributes: inheritPrototype(Student_2011, HNU_student); , the sub-constructor prototype becomes an instance of the superconstructor prototype, and the properties in the superconstructor prototype are shared with the sub-constructor.
Private: HNU_student. call(this, name); The superconstructor is called to create a private property when the instance is created from the child constructor.

Other ways to create objects

Dynamic prototype pattern


function HNU_student(name) {
  this.name = name;
  this.sayName = function() {
    return this.name;
  };

  if (!HNU_student.prototype.school) {
    HNU_student.prototype.school = 'HNU';
    HNU_student.prototype.saySchool = function() {
      return this.school;
    };
  }
}

var hiyohoo = new HNU_student('xujian');

The shared properties defined in the stereotype are put into the constructor, and the stereotype shared properties are initialized the first time the constructor is called to create the instance, using a judgment statement.

The parasitic constructor pattern


function SpecialArray() {
  var values = new Array();
  values.push.apply(values, arguments);
  values.toPipedString = function() {
    return this.join('|');
  };

  return values;
}

var colors = new SpecialArray('red', 'black', 'white');

Used to add special properties to the native constructor.

Other ways of inheriting objects

Combination of inheritance


function HNU_student(name) {
  this.name = name;
  this.sayName = function() {
    return this.name;
  };
}
HNU_student.prototype.school = 'HNU';
HNU_student.prototype.saySchool = function() {
  return this.school;
};
function Student_2011(name, number) {
  HNU_student.call(this, name);
  this.number = number;
  this.sayNumber = function() {
    return this.number;
  };
}
Student_2011.prototype = new HNU_student();
Student_2011.prototype.constructor = Student_2011;
Student_2011.prototype.graduationTime = 2015;
Student_2011.prototype.sayGraduationTime = function() {
  return this.graduationTime;
}
var hiyohoo = new Student_2011('xujian', 20110803203);

Student_2011.prototype = new HNU_student(); , the prototype of the child constructor points to the prototype of the superconstructor, and the instance finds all the shared properties through the prototype chain.
HNU_student.call (this, name); HNU_student.call (this, name); The superconstructor is called to create a private property when the instance is created from the child constructor.

Bug: The superconstructor is called twice. Student_2011.prototype = new HNU_student(); At the same time, private properties defined by the superconstructor are created in the child constructor prototype. Private properties in these prototypes are overridden by properties of the same name in the instance.

Original type inheritance, parasitic inheritance


function object(o) {
  function F() {}
  F.prototype = o;
  return new F();
}
var student1 = {
  school: 'HNU',
  saySchool: function() {
    return this.school;
  }
};
var student2 = object(student1);

Object.creat () is a new method in ES85en5 that takes two arguments: 1 is the original object that is used as the prototype, and 2 is an object that overwrites or adds attributes, acting the same way as the custom object().


var student1 = {
  name: 'xujian',
  school: 'HNU'
};
var student2 = Object.create(student1, {
  name: {
    value: 'huangjing'
  }
});

Parasite inheritance enhances the object by adding additional attributes to the original type inheritance.


function object(o) {
  function F() {}
  F.prototype = o;
  return new F();
}
function creatAnother(original) {
  var clone = object(original);
  clone.sayHi = function() {
    alert('Hi!');
  };
  return clone;
}
var student1 = {
  school: 'HNU',
  saySchool: function() {
    return this.school;
  }
};
var student2 = creatAnother(student1);

Primitive inheritance and parasitic inheritance are used to create instance objects that are similar to existing objects.


Related articles: