JS Object-Oriented Programming implementation

Source: Internet
Author: User

A Function is a special object, and its particularity is reflected in its multiple identities.

A Function can declare a common Function, which is the same as the Function concept in other languages. In addition, a Function can be used as a type declaration and implementation, an object constructor, and a class reference.

The Apply and Call methods can bind functions to other objects for execution.

Use (... In ...) The statement can traverse all attributes and methods of an object. The following example traverses the attributes and methods of the test1 object. If it is an attribute that has just been output, if it is a method that has just been executed.

Function test1 ()

{

This. p1 = "p1 ";

This. p2 = "p2 ";

This. f1 = function ()

{

Alert ("f1 ");

}

This. f2 = function ()

{

Alert ("f2 ");

}

}

Var obj1 = new test1 ();

// Traverse all attributes of t

For (p in t)

{

// If it is a function type, just execute this function

If (typeof (t [p]) = "function ")

T [p] ();

// Output other types of attributes

Else alert (t [p]);

}

1. Class Declaration

(1) Use the this keyword

Function test1 ()

{

This. p1 = "p1 ";

This. p2 = "p2 ";

This. f1 = function ()

{

Alert ("f1 ");

}

This. f2 = function ()

{

Alert ("f2 ");

}

}

In, member variables do not have private and public mechanisms, but you can declare local variables by using var within the class. Its scope is inside the class definition.

 

(2) class declaration using ptototype

To declare an instance attribute or method of a class, you can use the prototype attribute of the object in. For example:

Test1.prototype. prop2 = "prop2 ";

Test1.prototype. method2 = function (){

Alert (this. prop2 );

}

Declare the class using the prototype attribute, as shown below:

Function test ()

{

}

Test. prototype = {

P1: "p1 ";

P2: "p2 ";

F1: funciton ()

{

Alert ("f2 ");

}

}

2. Inheritance

The inheritance syntax is not supported, but the inheritance can still be implemented by copying attributes and objects.

Function test2 (){}

For (p in test. prototype)

{

Test2.prototype [p] = test. prototype [p];

}

Test2.prototype. newMethod = function ()

{

Alert ("newMethod ");

}

You can also refer to the Prototype framework to implement the inheritance method:

Object. extend = function (destination, source)

{

For (property in source)

{

Destination [property] = source [property];

}

Return destiantion;

}

Function test2 (){}

Test2.prototype = object. extend ({newMethod: function ()

{Alert ("newMethod ");}

},

Test. prototype

);

3. Polymorphism

The implementation of polymorphism can adopt methods similar to inheritance. The first abstract class is defined. You can call some virtual methods. The virtual methods are not defined in the abstract class, but are implemented through its specific implementation class. For example:

Object. extend = function (destination, source)

{

For (property in source)

{

Destination [property] = source [property];

}

Return destiantion;

}

// Define an abstract base class

Function base (){}

Base. prototype = {

Initialize: function (){

This. oninit (); // a virtual method is called.

}

}

 

Function test ()

{

// Constructor

}

 

Test. prototype = object. extend (

{

Prop: "prop ",

Oninit: function ()

{

Alert (this. prop)

}

}, Base. prototype

);

 

Function test2 ()

{

}

 

Test2.prototype = object. extend (

{

Prop2: "prop2 ";

Oninit: function ()

{

Alert (this. prop2 );

}

}, Base. prototype

}

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.