JS's encapsulation, inheritance, polymorphism

Source: Internet
Author: User

JS is a very flexible language, do not talk about polymorphism (or that it is polymorphic)
Closed part, no direct access to the outside world
Indirect access to private parts through open sections
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
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.