JS is a very flexible language, do not talk about polymorphism (or that it is polymorphic)
Packaging
Concept:
Closed part, no direct access to the outside world
Indirect access to private parts through open sections
Example:
Not encapsulated: All properties of the constructor are open
function Girl (NAME,BF) { this. Name = name; this. bf = BF;} var New Girl ("Lin Daiyu", "Jiabaoyu" + "Love" + GIRL.BF); // lin Daiyu love Jia Baoyu
Encapsulation: Encapsulates part of a property to be accessed by a public interface
function Girl (NAME,BF) { var secret = BF; this . Name = name; this . Showlove = function () { return secret; var girl = new Girl ("Lin Daiyu", "Jia Baoyu") Span style= "color: #000000;" ); alert (girl.name + "Love" + Girl.secret); // Lin Daiyu love Undefined,secret not accessible alert (girl.name + "Love" + Girl.showlove ()); // Lin Daiyu love Jia Baoyu, access to private properties indirectly through the open section
Summary: Complete encapsulation of private properties with closures
Inherited
First, the prototype inheritance
//the core of the prototype chain is __proto__ functionDog () { This. Bark =function() {alert ("Wangwang") } } functionHashiqi () {} Hashiqi.prototype=NewDog (); varDog1 =NewHashiqi (); Dog1.bark ();//WangwangHashiqi.prototype.bark=function() {alert ("Wuwu"); } varDOG2 =NewHashiqi (); Dog2.bark ();//Wuwu varDog3 =NewDog (); Dog3.bark ();//Wangwang
Second, the object impersonating
Temp variable
function Parent () {} function Child () { this. temp = Parent; This . Temp (); Delete This . temp;}
Call () and apply ()
function Parent () {} function Child () { Parent.call (this, var1,var2 ...) // parent.apply (This,[var1,var2 ...])}
Iii. copying and inheriting
Object.prototype.extend =function(obj) { for(varKeyinchobj) { if( This[key]==undefined) { This[key]=Obj[key]; } }}varCat = {color: "Yellow", climb:function() {alert ("I will climb the tree");}}varTiger = {color: "Yellow and black"}tiger.extend (cat); tiger.climb ();//I can climb trees .alert (Tiger.color);//Yellow and Black
Iv. Mixed inheritance
Static methods
// this is a constructor and an object function Bird () { this. wings=2; This function () { alert ("I Will Fly"); }}// This is a static method of the bird object and belongs to this object function only () { alert ("Eat Bug")}varnew Bird (); Bird instance, cannot access eat ()
JS's encapsulation, inheritance, polymorphism