JS Advanced --- inheritance

Inheritance
of object-oriented programming ideas: According to the requirements, analysis objects, found objects what characteristics and behaviors to achieve demand through the code, in order to achieve this requirement, it is necessary to create an object, in order to create an object, you should have a constructor display then the object created by a constructor, to achieve the corresponding functions and requirements by calling the object properties and methods, you can

need to pay attention
first of all, JS is not an object-oriented language, JS is an object-based language, so why learn js also learn object-oriented, because the object-oriented thinking is suitable for people's thoughts, it will be more convenient programming, and maintenance of late ....
the concept of object-oriented programming languages have a class (class) are (also a special data types), but JS not object-oriented language, therefore, JS no class (class), but JS can be simulated object-oriented programming ideas, JS will be used to simulate the concept of class (class) by the constructor

object-oriented properties: encapsulation, inheritance, polymorphism
package: package is
a value stored in a variable - packaging
cook function code in a repeat - Means
a set of properties in one object - the package
some similar features functions (methods) in one object - the package
many similar objects in a file js --- package


inheritance: 
First Inheritance is a the relationship between the species relationship, type (class) and class, JS no class, but can be simulated by the constructor class, then the prototype inheritance by
inheritance is for data sharing, js Inheritance also to achieve data sharing
prototype effect one: data sharing, saving memory space
prototype role of the two: In order to achieve inheritance

Inheritance is a relationship:
The relationship between the parent level and subclass level of

polymorphism:
an object behave differently, or the same behavior for different objects, different results, to how state, we must first have inherited, js can simulate polymorphism, but not to use, it will not simulate



inheritance example:
copy Code
    // example:
    // person has the name, gender, age, eat, sleep, play
    // student, has the name, gender, age aCHIEVEMENTS, eat, sleep, play, learning behavior


    // js be achieved through inheritance prototype

    function the Person (name, Age, Sex) {
      this.name = name;
      this.sex = Sex;
      this.age = Age;
    }
    = function Person.prototype.eat () {
      the console.log ( "person can eat");
    };
    Person.prototype.sleep = function () {
      the console.log ( "sleeping person");
    };
    Person.prototype = function .play () {
      console.log ( "life is not the same play it");
    };


    Student function (Score) {
      this.score = Score;
    }
    // change to point to the prototype Students ==========> students and IR have occurred
    Student.prototype = new Person ( "Bob" , 10, "M");
    Student.prototype.study = function () {
      the console.log ( "very tired of learning oh.");
    };

    // much the same code, resulting in redundant code (repetition code)

    var = new new STU Student (100);
    the console.log (stu.name);
    the console.log (stu.age);
    the console.log (stu.sex);
    stu.eat ();
    stu.play ();
    stu.sleep ();
    console.log ( "the following are some students object yourself");
    console.log (stu.score);
    stu.study ();
copy the code


after the change point to the students of the prototype ==========> students and human relations has taken place, resulting in the prototype chain, which was the student of the properties and methods as well as their properties and methods

Published 157 original articles · won praise 43 · views 90000 +

Guess you like

Origin blog.csdn.net/qq_39581763/article/details/103902668