Javascript Object-Oriented Programming

Source: Internet
Author: User
Tags natural logarithm

Javascript importance
Usage
1. In web applications, javascript is used in front-end interface programming;
2. Web2.0 and Ajax promote the javascript language.
3. As a large number of c/s applications turn to B/s, the rich client technology will be continuously promoted, and the application scope of javascript language will continue to increase;
Features of javascript
Simple
Dynamic
Object-based (Object-Oriented)

Javascript object-oriented Overview
Javascript is a dynamic scripting language for (based) objects. It is a scripting language based on Object and EventDriven and has security performance. It has various characteristics specific to object-oriented languages, such as encapsulation, inheritance, and polymorphism. But for most people, we only use javascript as a functional language, and only use it for some simple front-end data input verification and some simple page dynamic effects, we cannot fully grasp the features of dynamic languages.
In many excellent Ajax frameworks, such as ExtJS and JQuery, a large number of javascript object-oriented features are used. ext technology and advanced features of javascript must be used, object-oriented language features must be fully grasped.

Knowledge about Javascript
Development History of Javascript
Three major components of Javascript
ECMAScript
Syntax \ Data Type \ statement \ keywords \ reserved words \ operator \ object
DOM (Document Object Model)
BOM (Browser Object Model)

Discussion on the flexible features of JavaScript
1. Spiritual test of Dynamic Language
Javascript, as a dynamic language, has a very flexible sending. In the process of using Javascript, You need to flexibly master and apply its dynamic features to be handy.
Think about the output below
Copy codeThe Code is as follows:
Function Animal (name ){
This. name = name;
This. age = 0;
};
Var a1 = Animal; // output:
Var a2 = Animal (); // output:
Var a3 = new Animal (); // output:
Var a4 = new Animal; // output:


Data Types in Javascript
Basic Data Type
Number (Numbers)
String (Strings)
Boolean
Special values (null, undefined, NaN ).
Object Type
Objects are complex data types. Objects can contain basic types, objects, and functions. arrays are an object type. For javascript, it can be said that everything is an object, including a class !.
Var c = new Object ();
1. digit type
The numeric type is the basic data type in all languages. The numeric types in javascript mainly include INTEGER (Int) and Float (Float, however, both types are saved in the memory as floating points. The numeric type usually appears in a program in the form of a numeric constant in javascript. Generally, it is based on the 10-digit data, which is composed of the 10 digits 0-9, for example, the hexadecimal data starting with 0x (consisting of 16 characters including 0-9 and a to f, for example, converting 0xff to a 10-digit system is 255 (that is, 15*16 + 15 = 255). Some javascript implementations also support 8-digit data, that is, the data starting with 0 (consisting of the eight numbers 0-7). For example, if the octal value of 0377 is converted into a decimal number, it is 255, that is, (3*64 + 7*8 + 7 = 255 ).
2. Character Type
A string consists of various characters, numbers, and special strings. You can directly use single quotes or double quotes in a program to generate a String constant. There cannot be carriage return characters in the string. To include the carriage return character in the string, use the Escape Character \ n. Below are some simple string constants:
"" // The empty string: it has zero characters
'Testing'
"3.14"
'Name = "myform "'
"Wouldn't you prefer O 'Reilly's book? "
"This string \ nhas two lines"
"π is the ratio of a circle's circumference to its diameter"

3. Boolean
Boolean is used to represent true or false. In javascript, when used for Boolean operations, data except 0, null, null, undefined, NaN, etc. all represent true.
If (0 | "" | false | null | undefined | NaN) alert ("A condition returns true ");
Boolean constants are only false and true. False and True are not constants.

4. Object Type
Javascript is an object-based language, and objects are its core.

Program Process Control
Sequential Structure
If condition selection statement
Switch selection statement
While loop statement
Do while statement
For Loop statement
Break and continue statements

For... in loop statement
For (variable in set or object)
{
Execution statement Block
}
Copy codeThe Code is as follows:
<Script language = "javascript">
Var as = [1, 4, 5, 6], output = "";
For (var x in)
{
Output + = "x =" + as [x];
}
Alert (output );
</Script>
Var as = {id: 5, name: 'test '};
For (var x in)
{
Output + = x + "=" + as [x];
}
Alert (output );

Logical operators
&&
Logic and. If both the left and right operands are true, the return value is true. Otherwise, false is returned.

Logic or, when both the left and right operands are false, the first value or false is returned.
!
Non-logical. If the operand is true, the return value is false. Otherwise, true is returned.
Note:
In logical operations, 0, "", false, null, undefined, and NaN indicate false.

Function Definition and calling
The format of defining a function is as follows:
Function Name (parameter list)
{
Program code
Return expression;
}
Copy codeThe Code is as follows:
<Script language = "javascript">
Var msg = "global variables ";
Function square (x, y)
{
Var sum;
Sum = x * x + y * y;
Return sum;
}
Function show ()
{
Var msg = "local variable ";
Alert (msg );
}
// Var sum;
Alert ("sum =" + sum );
Sum = square (2, 3 );
Alert ("sum =" + sum );
Show ();
</Script>

Undefined
Alert ("sum =" + square (2, 3 ));

You can call a function in the following ways:
Function Name (parameter 1 passed to the function, parameter 2 passed to the function ,....)
Variable = function name (parameter 1 passed to the function, parameter 2 passed to the function ,....)
For function calls with returned values, you can also directly use the returned results in the program, for example, alert ("sum =" + square (2, 3 ));
Undefined is returned if no function value is specified.

Parameter variability of functions (arguments)
Copy codeThe Code is as follows:
<Script language = "javascript">
Function sum ()
{
Var s = 0;
For (var I = 0; I <arguments. length; I ++)
S + = arguments [I];
Return s;
}
Sum (1, 2 );
Sum (3, 4, 5 );
</Script>

Up to 255. The length of the function object can return the number of parameters that the function wants to provide.
Variability of function parameters
Function add (s, B ){
If (s) alert ("the first parameter is:" + s );
If (! B) alert ("no second parameter !");
Else alert ("the second parameter is:" + B );
}
Arguments
Arguments is an object similar to an array but not an array. It is similar to an array because it has the same access nature and method as an array, you can use arguments [n] to access the values of a single parameter and have the length attribute of the array length.
How can I write a method to achieve the sum of any number?
Alert (sum (1, 2, 3); // output 6
Alert (sum (100,200,500,900); // output 1700

Create a Function using the Function class
Basic syntax format for creating dynamic functions:
Var varName = new Function (argument1,..., lastArgument );
Note:
All parameters must be string-type, and the final parameter must be the functional program code of this dynamic function.
Example:
Copy codeThe Code is as follows:
<Script language = "javascript">
Var square = new Function ("x", "y ",
"Var sum; sum = x * x + y * y; return sum ;");
Alert (square (3, 2 ));
Var alsoDoSquare = doAdd;
Alert (alsoDoSquare (3, 2 ));
</Script>


Think twice:
What is the function of a dynamic function? Under what circumstances.

Closure (closure)
The Javascript closure stores a copy of the variable (key-Value Pair) it obtains from the upper-level function or scope in another scope ), however, these key-value pairs will not be destroyed as the upper-level functions are executed.
In this way, after var c = a () is executed, variable c actually points to function B, variable I is used in B, and then c () is executed () then a window will pop up showing the I value (the first time is 1 ). This Code actually creates a closure. Why? Because variable c outside function a references function B in function a, that is:

When function a's internal function B is referenced by a variable outside function a, a closure is created ".
Copy codeThe Code is as follows:
Function (){
Var I = 0;
Function B (){
Alert (++ I );
}
Return B;
}
Var c = ();
C ();

The function of the closure is that after a is executed and returned, the closure makes the garbage collection mechanism of Javascript GC not to reclaim the resources occupied by, because the execution of the internal function B of a depends on the variables in.

Function scope and this
1. You can use this in a function or method to reference the current object of the function.
2. When the current object of the function is not explicitly specified, the scope is window.
3. call and apply can be used to dynamically change the function execution scope.

Copy codeThe Code is as follows:
Var b1 = {v: "this is b1 "};
Var b2 = {v: "this is b2 "};
Function B (d ){
Alert (this. v );
}
B (); // output:
Window. B (); // output:
B. call (b1); // output:
B. apply (b2); // output:


Lexcical scope ). In general, the scope of javascript variables is determined during definition rather than execution. That is to say, the lexical scope depends on the source code and can be determined by the compiler through static analysis, therefore, lexical scopes are also called static scopes ). However, it should be noted that the semantics of with and eval cannot be implemented only through static technology, so it can only be said that the javascript scope mechanism is very close to the lexical scope ).
When executing each function instance, the javascript engine creates an execution environment (execution context ). The execution environment contains a call object)
The called object is a scriptObject structure (scriptObject is a static system related to the function, consistent with the life cycle of the function instance ), it is used to save the syntax analysis structures of the internal variable table varDecls, built-in function table funDecls, parent Reference List upvalue, and so on (note that varDecls and funDecls are obtained in the syntax analysis phase, and save it in the syntax tree. When the function instance is executed, the information is copied from the syntax tree to scriptObject ).

Apply and call: they are used to bind a function to another object for running. The two are different only when defining parameters:
Apply (thisArg, argArray );
Call (thisArg [, arg1, arg2…] ]);
That is, the this pointer inside all functions will be assigned to thisArg, which can be used to run functions as methods of another object.

Description of apply
If argArray is not a valid array or an arguments object, a TypeError occurs. If neither argArray nor thisArg is provided, the Global object will be used as thisArg and cannot be passed with any parameters.

Call description
The call method can change the object context of a function from the initial context to the new object specified by thisArg. If the thisArg parameter is not provided, the Global object is used as the thisArg

The point ):
There is another technique in applying call and apply. After applying call and apply to another function (class), the current function (class) has another function (class) method or attribute.
In the javascript browser, the object scope is window by default.
C. run ();
Window. c. run ();

System functions in JavaScript (Global class)
EncodeURI and encodeURIComponent Methods
Returns the encoded result of a URI string.
DecodeURI and decodeURIComponent () Methods
Decodes an encoded URI string into the initial string and returns it.
ParseInt Method
Converts a string to an integer in the specified hexadecimal format: parseInt (numString, [radix]). If the second parameter is not specified, the string prefixed with '0x 'is considered as hexadecimal, and the string prefixed with '0' is considered as octal, all other strings are treated as decimal.
ParseFloat Method
Converts a string to a decimal number.
IsNaN Method
Checks whether the return values of parseInt and parseFloat methods are NaN.
Escape Method
Returns the encoded result string. All spaces, punctuation marks, accents, and any other non-ASCII characters are replaced by % xx encoding, where xx equals to the hexadecimal number of the Unicode encoding of the character, characters with a character value greater than 255 are stored in % uxxxx format.
Unescape Method
Decodes a result string encoded using the escape method into the original string and returns it.
Eval Method
Execute the parameter string as a JavaScript expression.

JavaScript internal class
Dynamic Object
Use the format of "object instance name. member" to access its attributes and methods.
Static object
Use the format of "Object Name. member" to access its attributes and methods.

Object Class (Object)
Number class (object)
String class (object)
Math class (object)
Date class (object)
ToString Method

Object Class
The Object class is the base class of all javascript classes. It provides a simple way to create custom objects without the need for programmers to define constructors.
Main attributes:
Constructor-object constructor
Prototype-obtain the prototype object of the class, which is static.
Main Methods:
HasOwnProperty (property)-whether it belongs to the property defined in this class
IsPrototypeOf (object)-whether it is the prototype of the specified class
PropertyIsEnumerable (property)-whether to enumerate attributes
ToString ()-returns the string corresponding to the object
ValueOf ()-return the original type value corresponding to the object

<Script language = "javascript">
Function getAttributeValue (attr)
{
Alert (person [attr]);
}
Var person = new Object ();
Person. name = "zs ";
Person. age = 18;
GetAttributeValue ("name ");
GetAttributeValue ("age ");
</Script>

Number
The Number class represents the data class, including some static members and numerical processing methods.
Static attributes:
MAX_VALUE, MIN_VALUE, NEGATIVE_INFINITY, POSITIVE_INFINITY, and NaN
Main Methods:
ToFixed (n)-the number of decimal places, automatically rounded
ToPrecision (n)-whether it is the prototype of the specified class
PropertyIsEnumerable (property)-whether to enumerate attributes
ToString ()-returns the string corresponding to the object
ValueOf ()-return the original type value corresponding to the object

Copy codeThe Code is as follows:
<Script language = "javascript">
Var oNumberObject = new Number (99 );
Alert (oNumberObject. toFixed (2); // outputs "99.00"
</Script>


String type
Length attribute
Anchor, big, bold, fontcolor, link, and other methods
CharAt Method
Note: The index position of the first character in a string is 0, and so on.
CharCodeAt Method
Note: The returned result is the unicode encoding of the character.
Concat method, connection string
IndexOf method and lastIndexOf Method
Match and search methods
Replace and split Methods
Slice Method
Note: str1.slice (0) and str1.slice (0,-1) both return the entire string.
Substr and substring Methods
The content returned by the substring method does not contain the ending character.
ToLowerCase and toUpperCase Methods

Math class
Attribute:
E, representing the mathematical constant e, which is approximately 2.718.
LN10 represents the natural logarithm of 10, which is approximately 2.302.
LN2 represents the natural logarithm of 2, which is approximately 0.693.
PI, which represents the value of the Gini coefficient of the mathematical constant, approximately 3.14159.
SQRT1-2, representing one of the square root of 2, about equal to 0.707.
SQRT2 indicates the square root of 2, which is approximately 1.414.

Method:
Abs method returns the absolute value of a number.
Sin and cos Methods return the sine and Cosine values of numbers respectively.
The asin and acos Methods return the arcsin and arccosine values of numbers respectively.
The random method returns a pseudo-random number between 0 and 1.
The Math object is a static class. You cannot use the new keyword to create an object instance. You should directly use the object name. to access its attributes or methods, for example, var num = Math. random ();

Date class
The toGMTString method returns the string form of the Date represented by the Date object instance. The string uses the GMT format, for example, "05 Jan 1996 00:00:00 GMT ".
GetYear, getMonth, getDate, and getDay Methods
GetHours, getMinutes, getSeconds, and getMilliseconds
The getTime method returns the number of milliseconds from 00:00:00, January 1, January 1, 1970 to the time represented by the Date object instance.
Copy codeThe Code is as follows:
<Script language = "javascript">
Var current_time = new Date ();
Var strDate = current_time.getYear () + "year ";
StrDate + = current_time.getMonth () + "month ";
StrDate + = current_time.getDate () + "day ";
StrDate + = current_time.getHours () + ":";
StrDate + = current_time.getMinutes () + ":";
StrDate + = current_time.getSeconds ();
Alert (strDate );
</Script>

Constructor: Date (), Date (dateVal), Date (year, month, date [, hours [, minutes [, seconds [, MS])
The parse method analyzes a string that represents the date and time and returns the time value it represents. The value is represented by a millisecond value counted from 00:00:00, January 1, January 1, 1970. The parse method is a static method.

ToString Method
The toString method is a member method of all internal objects in JavaScript. Its main function is to convert the data in the object to a string in a certain format. The specific conversion method depends on the object type.

Example:
Copy codeThe Code is as follows:
<Script language = "javascript">
Var x = 328;
Alert ("hex =" + x. toString (16) + "bin =" + x. toString (2 ));
</Script>

Array class
Three constructor methods:
Array ()
Array (4)
Array (3.5, "abc", 3)

Array sorting example:
Copy codeThe Code is as follows:
<Script language = "javascript">
Var arr = new Array ();
Arr [0] = 3.5;
Arr [1] = "abc"
Arr [2] = 3;
Arr. sort ();
Var x, str = "";
For (x in arr)
{
Str + = x + ":" + arr [x] + "\ n ";
}
Alert (str );
</Script>

Attributes and methods of the Array class
Length-get the length of the array;
Concat-connection array;
Join-converts an array to a string;
Pop-an element pops up;
Push-put an element;
Reverse-reverse the order of elements in the data;
Shift-remove the first element;
Slice-truncated array;
Sort-sorting array;
Unshift-append an element to the front;

Implement Arrays Using objects
Copy codeThe Code is as follows:
<Script language = "javascript">
Function MyArray ()
{
This. length = arguments. length;
For (var I = 0; I <this. length; I ++)
{
This [I] = arguments [I];
}
}
Var str = "";
Var arr = new MyArray (4, 3.5, "abc ");
For (var I = 0; I <arr. length; I ++)
{
Str + = arr [I] + "\ n ";
}
Alert (str );
</Script>
<Script language = "javascript">
Function MyArray (size)
{
This. length = size;
For (var I = 0; I <size; I ++)
{
This [I] = "";
}
}
Var arr = new MyArray (2 );
Arr [0] = 3;
Arr [1] = "abc ";
Arr [2] = 4;
Var x, str = "";
For (x in arr)
{
Str + = x + ":" + arr [x] + "\ n ";
}
Alert (str );
</Script>


User-Defined classes and objects
1. Factory method-use new Object to create an Object and add relevant attributes;
2. Use constructors to define classes.
3. Use prototype
4. constructor and prototype hybrid mode
5. Dynamic Prototype
Instance
Car class (object)
Attribute:
Color-color
Doors-number of doors
Price-price
Drivers-driver
Method:
ShowColor-display the vehicle color

Typeof and instanceof Operators
The delete operator is used to delete a specified member of an object.
Typeof xx-string returns the type or undefined of xx object.
Var d = 7.5;
Alert (typeof d );
Alert (typeof d2 );
Alert (typeof new Object ());
Alert (typeof Object );

Xx instanceof class name, return boolean type:
Copy codeThe Code is as follows:
<Script language = "javascript">
Var o = new String ("AB ");
Alert (o instanceof String );
Alert (o instanceof Number );
Alert (o instanceof Object );
</Script>


Delete and void Operators
The delete operator is used to delete a specified member of an object.
Var d = new Object ();
D. p1 = "this is p1 ";
Alert (d. p1 );
Delete d. p1;
Alert (d. p1 );
Delete can only delete user-defined members.
Delete d. toString;
Alert (d. toString ());
Void is used to convert any number to undefined.
Var d = new Object ();
Alert (void (d ));
Use Cases:
<A href = "javascript: window. open ('about: blank ')"> Click Me </a>

Class Modification
1. prototype details
2. Add new methods to existing classes
3. Redefinition of class methods
4. Super post binding

Prototype is an attribute of the Function object. When we access a member of the object, we first find it inside the object. If it cannot be found, we will find it in the prototype object of the class where the object is located.

Encapsulation
Encapsulation: encapsulation, that is, encapsulating objective objects into abstract classes, and classes can only operate trusted classes or objects on their own data and methods, hide untrusted information.
In javascript, closures can be used for encapsulation and code examples.
A simple example of javascript Public Member definition, Private member definition, and privileged method definition is provided!
Copy codeThe Code is as follows:
<Script>
// Define a javascript class
Function JsClass (privateParam/**/, publicParam) {// Constructor
Var priMember = privateParam; // Private variable
This. pubMember = publicParam; // public variable
// Define a private Method
Function priMethod (){
Return "priMethod ()";
}
// Define the privileged method
// The privileged method can access all members.
This. privilegedMethod = function (){
Var str = "this is a privileged method. I called \ n ";
Str + = "private variable:" + priMember + "\ n ";
Str + = "Private method:" + priMethod () + "\ n ";
Str + = "Public variable:" + this. pubMember + "\ n ";
Str + = "public method:" + this. pubMethod ();

Return str;
}
}
// Add public methods
// Private variables and methods cannot be called
JsClass. prototype. pubMethod = function (){
Return "pubMethod ()";
}

// Instances using JsClass
JsObject = new JsClass ("priMember", "pubMember ");

// Alert (JsObject. pubMember); // The pubMember information is displayed.
// Alert (JsObject. priMember); // The undefined information is displayed.
// Alert (JsObject. pubMethod (); // The pubMethod information is displayed.
// Alert (JsObject. priMethod (); // The error "the object does not support this attribute or method" is displayed.
Alert (JsObject. privilegedMethod ());
</Script>



A simple example of javascript Public Member definition, Private member definition, and privileged method definition is provided!
Copy codeThe Code is as follows:
<Script>
// Define a javascript class
Function JsClass (privateParam/**/, publicParam) {// Constructor
Var priMember = privateParam; // Private variable
This. pubMember = publicParam; // public variable
// Define a private Method
Function priMethod (){
Return "priMethod ()";
}
// Define the privileged method
// The privileged method can access all members.
This. privilegedMethod = function (){
Var str = "this is a privileged method. I called \ n ";
Str + = "private variable:" + priMember + "\ n ";
Str + = "Private method:" + priMethod () + "\ n ";
Str + = "Public variable:" + this. pubMember + "\ n ";
Str + = "public method:" + this. pubMethod ();

Return str;
}
}
// Add public methods
// Private variables and methods cannot be called
JsClass. prototype. pubMethod = function (){
Return "pubMethod ()";
}

// Instances using JsClass
JsObject = new JsClass ("priMember", "pubMember ");

// Alert (JsObject. pubMember); // The pubMember information is displayed.
// Alert (JsObject. priMember); // The undefined information is displayed.
// Alert (JsObject. pubMethod (); // The pubMethod information is displayed.
// Alert (JsObject. priMethod (); // The error "the object does not support this attribute or method" is displayed.
Alert (JsObject. privilegedMethod ());
</Script>


Inheritance
One of the main functions of Object-Oriented Programming (OOP) is "inheritance ". Inheritance refers to the ability to use all the functions of an existing class and extend these functions without re-writing the original class.
1. Object impersonating
2. call and apply
3. prototype chain
4. hybrid mode

Inheritance-object impersonating
Copy codeThe Code is as follows:
Function classA (name ){
This. name = name;
This. showName = function () {alert (this. name );}
}
Function classB (name ){
This. newMethod = classA;
This. newMethod (name );
}
Obj = new classA ("hero ");
ObjB = new classB ("dby ");
Obj. showName (); // print hero
ObjB. showName (); // print dby indicates that classB inherits the classA method.
Object impersonating can implement multiple inheritance, for example
Function classz (){
This. newMethod = classX;
This. newMethod ();
Delete this. newMethod;
}

However, if classX and classY have the same attributes or methods, classY has a high priority.

Inherit-call Method
The call method is similar to the classical object impersonating method. Its first parameter is used as the object of this, and other parameters are directly passed to the function itself.
Copy codeThe Code is as follows:
Function sayName (perfix ){
Alert (perfix + this. name );
}
Obj = new Object ();
Obj. name = "hero ";
SayName. call (obj, "hello ,");
Function classA (name ){
This. name = name;
This. showName = function () {alert (this. name );};
}
Function classB (name ){
ClassA. call (this, name );
}
ObjB = new classB ("bing ");
ObjB. showName (); // indicates that classB inherits the showName method of classA.

Inherit-apply Method
The aplly () method has two parameters. One is used as the this object and the other is an array of parameters passed to the function.
Copy codeThe Code is as follows:
Function sayName (perfix ){
Alert (perfix + this. name );
}
Obj = new Object ();
Obj. name = "hero ";
SayName. aplly (obj, new Array ("hello ,"));

Inheritance-prototype chain
Any attributes and methods of the prototype object will be passed to all instances of the corresponding class. The prototype chain uses this method to show inheritance.
Function classA (){}
ClassA. prototype. name = "hero ";
ClassA. prototype. showName = function () {alert (this. name )}
Function classB (){}
ClassB. prototype = new classA ();
Objb = new classB ()
Objb. showName (); // print hero indicates that B inherits the method.
Note that the parameter is not passed to the classA constructor when it is called. This is the standard practice of the prototype chain and ensures that the constructor does not have any parameters.
All attributes and methods of the subclass must appear after the prototype attribute is assigned a value, and the values assigned before it will be deleted. because the prototype attribute of the object is replaced with a new object, the original object added with the new method will be destroyed.

Inheritance-hybrid mode
The constructor attribute is defined using the impersonating method, and the object method is defined using the original method.
Copy codeThe Code is as follows:
Function classA (name ){
This. name = name;
}
ClassA. prototype. showName = function () {alert (this. name )}
Function classB (name ){
ClassA. call (this, name );
}
ClassB. prototype = new classA ();
ClassB. prototype. showName1 = function () {alert (this. name + "*****");};
Obj = new classB ("hero ");
Obj. showName ();
Obj. showName1 ();

In the classB constructor, you can call the call method to inherit the name attribute in classA and use the prototype chain to inherit the showName method of classA.

Method overloading in javascript
Method overload and overriding)
The method in Javascript itself is a variable parameter and does not support reload operations. However, we can freely detect the parameters of the method in the method body to achieve the effect of overloading. (Use a variable parameter or arguments to simulate an example of overloading ).
Overwrite, also known as rewrite, refers to the method defined in the subclass to replace the parent class.

Exploration of polymorphisn in javascript
Polymorphisn: a technique that allows you to set a parent object to be equal to one or more of its sub-objects, the parent object can operate in different ways based on the features of the sub-objects assigned to it. To put it simply, you can assign a pointer of the subclass type to a pointer of the parent class. Polymorphism is for another purpose-interface reuse! Polymorphism is used to ensure that a certain attribute of an instance of any type in the family tree is called correctly when the class is inherited and derived.
Var a = [a1, a2, a3];

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.