JavaScript OOP polymorphism, encapsulation, inheritance

Source: Internet
Author: User

Package

  Encapsulation is the abstraction of data and the operation of the data is packaged together, the data is protected in the internal,

Other parts of the program can operate on the data only through authorized operations (member methods).

The JS package has only two states, one is public and one is private.

 <Scripttype= "Text/javascript">       functionPerson (name, Agei, Sal) {//Public         This. Name=name; //Private        var Age=Agei; varSalary=Sal; }       varP1= NewPerson ('ZS',  -, 10000); Window.alert (P1.name+p1.age); </Script> 
Remember the difference between this and Var, this refers to the public meaning, and VAR is defined as the private meaning

The difference between adding a member method through a constructor and adding a member method by using the prototype method
1. The function assigned by the prototype method is shared by all objects.
2. The attributes assigned by the prototype method are independent. (If you do not modify the properties, they are shared)
3, it is recommended that if we want all objects to use the same function, it is best to use the prototype method to add functions, which saves memory.

In particular, we have learned to add methods to all objects through prototype, but this method cannot access the private variables and methods of the class.

polymorphic

function overloading of JS

This is the basis of polymorphism, although the JS function does not support polymorphism, but in fact the JS function is stateless, support arbitrary length, type parameter list.

If more than one function with the same name is defined, the last function will prevail.

<HTML> <Head> <Scripttype= "Text/javascript">   //For exampleDescription JS does not support overloading  /*function Person () {this.test1=function (A, b) {Window.alert (' function (b) ');     } this.test1=function (a) {Window.alert (' function (a) ');   }} var p1=new person ();   Overloading is not supported in JS.   But this will not be an error, JS will default to the last name of a function, can be seen as the back of the previous overlay.   P1.test1 ("A", "B"); P1.test1 ("a");*/ </Script> </HTML> 

So we have to use the global variable arguments can be implemented,arguments is an array of classes, by determining the length of the passed parameter can be accessed to the value of the parameter list.

Polymorphic definition: means that objects of different classes are allowed to respond to the same message. That is, the same message can be used in a variety of different ways depending on the sending object.

The white point is that a parent class is inherited by multiple subclasses, and then multiple subclasses can add their different properties and methods.

    function Doadd () {        if (arguments.length==1) {          alert (arguments[0]+10);        } else if (arguments.length==2) {          alert (arguments[0]+arguments[1]);        }      }      Doadd (10); //outputs "      Doadd (30,20);

Inheritance

Inheritance solves code reuse and allows programming to be closer to human thinking. When multiple classes have the same attributes (variables) and methods, you can abstract the parent class from these classes.

These same properties and methods are defined in the parent class, and all subclasses do not need to redefine these properties and methods, only by inheriting the properties and methods in the parent class.

How to implement inheritance in JS





Person.apply (this,arguments); By using the Apply Method (array)
Person.call (THIS,A,B,C); Through this method (property)
}

JavaScript OOP polymorphism, encapsulation, inheritance

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.