Study a day of object-oriented summary, divided into three categories
- -! The first part of the ritual is the concept of knowledge!!!
Object-oriented programming, we can think of him as we are making people. An object is a person who has arms and legs, which is the property or method of an object. Then through the user's operation instructions (click,hover, etc.) to call our JS (we make the villain), control our villain to perform different actions!
#_ # It seems I've explained enough abstraction ...
Of course, the biggest advantage of object-oriented is reusability, save code, improve development efficiency, down to complete my debut article ...
1. Factory mode
--# Learning this model is really very teachable, start how to write all have errors, finally by asking the next friend to understand, the original following example of var obj = new Object ();
The object in this sentence can not be changed, he is the JS object (I think so, if not please point out) Of course this sentence can also be written
var obj = {}; or var obj = new function () {};
From these several ways I think we should be able to guess that our Var obj is possible as long as it is equal to an object ({}).
1 functionBox (name,age,fuck) {2 varobj =NewObject ();//Windows Objects3Obj.name =name;4Obj.age =Age ;5Obj.run =function (){6 returnfuck;7 };8 returnobj;9 };Ten varbb = Box (' Xiao Ming ', ' 38 ', ' Beijing '); OneConsole.log (Bb.run ()); Beijing
Note: instances produced in Factory mode are not supported with prototype inheritance! Therefore, it is not recommended to use it frequently in order to maintain code later.
See an article saying, do not let use this or new when using this Factory mode ... A little confused, what will not be able to use this or new??
I really do not understand when this writing will be used .... Okay, no, it's not a struggle. Know this writing is good ....
2. Constructor function
It is also a way that you like to use (more perfect is the + prototype pattern mixed write),
1 functionDog () {2 This. cat = 123;3 This. Mao = 456;4 This. Stu =function(){5Console.log ( This. Mao);6 }7 };8 //Console.log (typeof dog);
3, prototype mode (continue the example of the above constructor)
1Dog.prototype.gou =function(){2Console.log ( This. Mao); 3 };4 varANI =NewDog ();5 //Ani.gou (); 4566 //Console.log (ANI); Dog {cat: 123, Mao: 456, stu: function, Gou: function}
2 and 3 Add up is the individual think is perfect open way .... Prototype + constructor.
For the first time summed up so much. Think of some chicken jelly ... How to say the wrong place, also please point out.
Hey? looks like the object-oriented still missing part of it ... there seems to be inheritance. Is there anything else besides inheritance? To study again tomorrow ... Go to dinner ....
JS's object-oriented----Encapsulation Chapter