The three characteristics of object-oriented: 1. Encapsulation: encapsulate the function into the object, the external only need to call, do not care about the internal implementation 2. Inheritance: an object has all the members of another object. 3. Polymorphism: Multiple states of the same object under different circumstances

1. The encapsulation

Code encapsulation avoids code redundancy, avoids variable contamination, and essentially takes the common code out and calls it where it needs to be and also in inheritance

2. Polymorphism

Multiple states of the same object in different situations

function Animal(name){ this.name = name; }; Var dog = new Animal(' dog '); Dog. eat = function(food){console.log(' woof woof, I am '+ this.name +' I ate '+ food); }; Var cat = new Animal(' Tom '); Cat. Eat = function(food){console.log(' meow meow meow meow, I am '+ this.name +' I ate '+ food); }; function Person(name){ this.name = name; }; Var p1 = new Person('小 小 '); P1. Work = function(animal,food){// The animal constructor receives an object instantiated by the animal constructor and calls the object's eat method animal.eat(food); }; P1. Work (dog,' big bone '); P1. Work (cat,' dry fish ');Copy the code

The essence of inheritance is that one object owns all the members of another object

Var wangjianlin = {house:{address:' Beijing ', price:100000000000}, car:{Pinpai :' Beijing ', price:5000000}}; Var wangsicong = {girlFrineds: [' beans get son ', 'snow pear', 'Lin update', 'Wang Jiangang]}. // Need: wangsicong object to have all the members of Wangjianlin console.log(wangjianlin); console.log(wangsicong); // The solution is to go through all the members of the wangjianlin(parent object) and add them to the wangsicong(child object). // for(var key in wangjianlin){// wangsicong[key] = wangjianlin[key]; / /}; // console.log(wangsicong); // Second way: replace the prototype // solution: use the parent object (Wangjianlin) as the child object constructor (SonWang) to get the prototype object // Disadvantages: // function SonWang(GRS){// this.girlfrineds = GRS; / /}; / / SonWang. Prototype. FlowerMoney = function () {/ / console log (' I am wangjianlin son, I'm will spend money to coax girls eat six dollars a malatang '); / /}; // // sonwang. prototype = wangjianlin; // // sonwang. prototype = wangjianlin; / / var wangsicong = new SonWang ([' beans get son ', 'snow pear', 'Lin update', 'Wang Jiangang']); // console.log(wangsicong); / / var smallMao = new SonWang ([' fan bingbing, 'zhao', 'Li Qin', 'songyun tan']); // console.log(smallMao); // Third way: hybrid (mix + replace prototype) most used // solution: // SonWang(GRS){this.girlfrineds = GRS; // SonWang(GRS){this.girlfrineds = GRS; / /}; / / SonWang. Prototype. FlowerMoney = function () {/ / console log (' I am wangjianlin son, I'm will spend money to coax girls eat six dollars a malatang '); / /}; // // iterate over all the members of the WangJianlin object and add them to the SonWang prototype (without overwriting the original default prototype, // function extend(method,father){// for(var key in father){// method. Prototype [key] = father[key]; / /}; / /}; // // hybrid inheritance // extend(SonWang, Wangjianlin); / / var wangsicong = new SonWang ([' beans get son ', 'snow pear', 'Lin update', 'Wang Jiangang']); // console.log(wangsicong); / / var smallMao = new SonWang ([' sora ', 'jizah teachers',' more teachers', 'white stone teachers']); // console.log(smallMao); /* The fourth way: */ function Person(name,age){//this:new creates an empty object this.name = name; this.age = age; }; Var p1 = new Person(' zhang3 ',18); console.log(p1); P1. GirlFrend = 'girl '; Function Student(name,age,score){//this:new create empty object console.log(this); Person.call(this,name,age); person.call (this,name,age); person.call (this,name,age); // this.name = name; // this.age = age; this.score = score; }; Var s1 = new Student(' s1 ', 60,100); console.log(s1);Copy the code