js object-oriented design pattern inheritance

//Class inheritance

// function Father(){
//     this.fartherValue = true;
// }
// Father.prototype.getFartherValue = function () {
//     return this.fartherValue;
// }
// function Child(){
//     this.childValue = false;
// }
// Child.prototype = new Father();
// Child.prototype.getChildValue = function(){
//     return this.childValue;
// }
// let a = new Child();
// console.log(a.getChildValue());
// console.log(a.getFartherValue());

//Disadvantages of class inheritance / / 1. The subclass instantiates the parent class through the prototype, inherits the parent class, and the parent class has common properties if
//Reference types will be instantiated and shared in subclasses, and other subclasses of the instantiated subclass will also be affected

// function Farther() {
//    this.books = ["javascript","html","css"];
//    this.name ="-";
// }
// function Child(){
//
// }
// Child.prototype  = new Farther();
// let c1 = new Child();
// let c2 = new Child();
// console.log(c1.books);
// console.log(c1.name);
// c1.books.push("design pattern");
// //c1.name.replace(/-/g,"hello"); why class inheritance is only affected by reference type search
// console.log(c2.books);
// console.log(c2.name);

**

//Constructor inheritance

**

//The / / call method points the Child pointer to the Farther, so the new Child's object can use the common attributes and methods in / / / / Farther, so the object cannot get the attributes and methods of Prototype / / / / in Farther, so the combination inheritance should be used

// function Farther(id) {
//     //Reference type common property
//     this.books = ['javascript','html',"css"];
//     //Common properties of basic data types
//     this.id= id;
// }
// //Parent prototype method
// Farther.prototype = {
//     showBooks:function () {
//         console.log(this.books);
//     }
// }
// function Child(id){
//     Farther.call(this,id);
//     this.name = "hello";

// }
// let c1 = new Child(10);
// let c2 = new Child(11);
// console.log("c1 "+c1.books);
// console.log(c1.name);
// console.log("c2 "+c2.books);
//c1.books.push("design pattern");
// console.log("c1 "+c1.books);
// console.log("c2 "+c2.books);
// // console.log(c1.showBooks());

//Combination inheritance

// function F(name){
//     this.books = ['html','css','javascript'];
//     this.name = name;
// }
// F.prototype.display = function(){
//     console.log(this.name);
// }
// function C(time,name){
//     F.call(this,name);
//     this.time = time;
// }
// C.prototype = new F();
// C.prototype.gettme = function(){
//     console.log(this.time);
// }
// let c1 = new C(2019,"yqf");
// let c2 = new C(2018,"yqf");
// console.log(c1.books);
// c1.books.push("zeji");
// console.log(c2.books);
// c1.display();
// c2.gettme();

//When using constructor inheritance, the above methods execute the constructor of the parent class. When implementing the inheritance of the subclass / / prototype, the parent class is called, and the constructor of the parent class executes both sides
//So archetypal inheritance comes into being

//Archetypal inheritance

// function inhertObject(o) {
// //Declare an over function object
// function F() {}
// F.prototype =o;
// return new F();
// }
// let book ={
//   name:"js book",
//   books:["css","html","javasript"]
// };
// let newbook1 = inhertObject(book);
// let newbook2 = inhertObject(book);
//  console.log(newbook1.books);
//  newbook1.books.push("hello");
//  console.log(newbook2.books);
//Parasitic pattern
// function inhertObject(o) {
// //Declare an over function object
//     function F() {}
//     F.prototype =o;
//     return new F();
// }
// let book ={
//     name:"js book",
//     books:["css","html","javasript"]
// };
// function createBook(obj) {
//     Let o = new inheritobject (obj); / / parasitic object
//     o.getName = function() {/ / extension object
//
//     }
//     return o;
// }
//  let newbook1 = createBook(book);
// let newbook2 = createBook(book);
// console.log(newbook1.books);
// newbook1.books.push("hello");
// console.log(newbook2.books);

//Total inheritor parasite combination class / / relative to the combination inheritance, the parasite combination inheritance does not use the
//Common property, he just defines an empty function as a buffer

function inhertObject(o) {
//Declare an over function object
    function F() {}
    F.prototype =o;
    return new F();
}
function inhertiPrototype(Child,Father) {
   // Copy a prototype copy of the parent class and save it in the variable
   let p = inhertObject(Father.prototype);
   p.constructor = Child;
//    The copied constructor is the pointer address to the farther object
//    So point the prototype object pointer of the copied p to Child
    Child.prototype =p;
}
function Father(name) {
    this.name = name;
    this.books =['html',"css","javascript"];

}
function Child(name) {
    this.time = "2019";
    Father.call(this,name);
}
//Parasitic inheritance parent prototype
inhertiPrototype(Child,Father);
let c1 = new Child("yqf");
let c2 = new Child("fqy");
console.log(c1.books);
c1.books.push("java");
console.log(c1.books);
console.log(c2.books);

Tags: Javascript Java

Posted on Thu, 07 Nov 2019 10:46:46 -0500 by brett