JS is not an object-oriented programming language is an object-based language
The concept of object-oriented programming language concept of the amount of the class, not an object-oriented language JS JS so there are no classes, but can be simulated object-oriented JS, JS will be simulated by the constructor of the class
inherit:
Inheritance is a relationship between a relationship class and class, JS classes but may not be simulated by a class constructor, and then reduced by inheritance
By prototype inheritance: .prototype = new subclasses of the parent class; change point prototype
borrow constructors Inheritance: Constructors name .call (current object, attributes, properties, attributes) primarily address attribute values duplication
composition Inheritance: borrow prototype inheritance + The constructor inheritance. Property values to solve problems and repeat method
copy inheritance: the properties and methods of a copy of the object to another object by way of the loop through
borrow constructors Inheritance: Constructors name .call (current object, attributes, properties, attributes) primarily address attribute values duplication
composition Inheritance: borrow prototype inheritance + The constructor inheritance. Property values to solve problems and repeat method
copy inheritance: the properties and methods of a copy of the object to another object by way of the loop through
Prototype inheritance:
// the JS inheritance be achieved by a prototype function the Person (name, Age, Sex) { the this .name = name; the this .age = Age; the this .sex = Sex; }; Person.prototype.eat = function () { Console .log ( "I eat" ); }; Person.prototype.sleep = function () { the console.log ( "I sleep" ); }; Person.prototype.play = function () { the console.log ( " We are playing a game. "); }; Function Student (Score) { the this .score = Score; }; // prototype to change the students' point ====> Students and human relations has taken place Student.prototype = new new the Person ( "guoguo" 18, "M" ); Student.prototype.study = function () { the console.log ( "I learn" ); }; var STU = new new Student (100 ); the console.log (stu.name); Console .log (stu.age); the console.log (stu.sex); the console.log (stu.score); stu.study (); stu.play (); stu.eat(); stu.sleep();
Prototypal inheritance flaw: Because the prototype implementation inheritance change while pointing directly initialize the properties inherited property values are the same, only to re-invoke the object's properties were reassigned
function Person(name,age,sex,weight){ this.name=name; this.age=age; this.sex=sex; this.weight=weight; }; Person.prototype.sayHi=function(){ console.log("你好"); }; function Student(score){ this.score=score; }; Student.prototype=new Person("guoguo",20,"男","55kg"); var stu1 = new Student(100); stu1.name="lt"; stu1.age=18; console.log(stu1.name,stu1.age,stu1.sex,stu1.weight,stu1.score); var stu2 = new Student(90); console.log(stu2.name,stu2.age,stu2.sex,stu2.weight,stu2.score); var stu3 = new Student(80); console.log(stu3.name,stu3.age,stu3.sex,stu3.weight,stu3.score);
Borrowing Constructors Inheritance:
function Person(name,age,sex,weight){ this.name=name; this.age=age; this.sex=sex; this.weight=weight; }; Person.prototype.sayHi=function(){ console.log("你好"); }; function Student(name,age,sex,weight,score){ //借用构造函数 Person.call(this,name,age,sex,weight); this.score=score; }; var stu1 = new Student("guoguo",18,"男","55kg",100); console.log(stu1.name,stu1.age,stu1.sex,stu1.weight,stu1.score); //stu1.sayHi(); var stu2 = new Student("lt",18,"男","55kg",90); console.log(stu2.name,stu2.age,stu2.sex,stu2.weight,stu2.score); var stu3 = new Student("guoguolt",18,"男","55kg",80); console.log(stu3.name,stu3.age,stu3.sex,stu3.weight,stu3.score);
Borrowing constructor inherited defects: methods parent category can not inherit
A combination of inheritance:
Prototypal inheritance + borrow constructor inheritance. Resolve property values and methods of duplication
function the Person (name, Age, Sex) { the this .name = name; the this .age = Age; the this .sex = Sex; }; Person.prototype.sayHi = function () { the console.log ( "2333" ); } ; function Student (name, Age, Sex, Score) { // borrow constructors: property values to solve problems repeated Person.call ( the this , name, Age, Sex); the this .score = Score; }; // change the prototype points - Inheritance Student.prototype = new new Person(); Student.prototype.study=function(){ console.log("学习"); }; var stu = new Student("果果",19,"男",100); console.log(stu.name,stu.age,stu.sex,stu.score); stu.sayHi(); stu.study(); var stu1 = new Student("guoguo",19,"女",110); console.log(stu1.name,stu1.age,stu1.sex,stu1.score); stu1.sayHi(); stu1.study();
Copy Inheritance:
The properties and methods of an object is copied to another object by way of looping through
// copy inherited function the Person () {}; Person.prototype.name = "guoguo" ; Person.prototype.age = 20 is ; Person.prototype.sex = "M" ; Person.prototype.Play = function () { Console .log ( "play" ); }; var obj2 = {}; // configuration Person prototypes in prototype, prototype is an object, name, age, sex, play all the object properties and methods // the among the attributes and methods of an object by way of looping through into another object for ( var Key in Person.prototype) { obj2 [Key] =Person.prototype[key]; }; console.dir(obj2); obj2.Play();
to sum up:
Prototype role: data sharing, inheritance. Purpose: To save memory space
Prototypal inheritance: the prototype of the change point
Borrowing Constructors Inheritance: mainly to solve the problem of duplicate attribute value
Combination Inheritance: Constructors prototype inheritance + borrow inheritance. Resolve property values and methods of duplication
Copy Inheritance: is the need to share the object properties or methods copied to another object by way of looping through