JavaScript Object-Oriented Programming Basics: Encapsulating _js Object-oriented

Source: Internet
Author: User
Tags abstract extend inheritance static class

For a long time (here I want to gloat to say), JS is "a decorative role, complete a very limited function, such as form verification, the language itself has been used as a procedural language, it is difficult to complete complex functions." ”。 But (I'm going to Shine and bitterly say), "and the advent of Ajax makes complex scripts a necessary part, which puts new demands on JavaScript programming, and many AJAX applications begin to exploit the JavaScript object-oriented nature, Make the logic clearer. In fact, JavaScript provides a perfect mechanism to implement object-oriented development ideas. ”。 The god of the forehead, originally did not want to learn, now have to bite the bullet to learn.
There's so much nonsense about objects here. We all know that the three main characteristics of object-oriented programming are encapsulation, inheritance, and polymorphism. The following three characteristics around these, record some learning experience.
OK, first from the encapsulation of the introduction, as we all know, the object is the most basic unit of encapsulation. Encapsulation prevents the effects of changes that are dependent on the program from being interdependent. Object-oriented encapsulation is clearer and more powerful than the encapsulation of traditional languages. The code is cheap. To see Simple codes:

Define a class by how the function is defined
function Class1 () {
Definition of class members and constructors
Here Class1 is both a function and a class. As a function, it can be understood as the constructor of the class, which is responsible for initializing the work.
}

Use the new operator to get an instance of a class
var obj = new Class1 ();
/* Throw away the concept of class, from the form of code, Class1 is a function, then is not all functions can be used to operate with new? The answer is yes.
In JavaScript, functions and classes are concepts that return an object when a function is new. If the class member is not initialized in this function, an empty object is returned.
In fact, when new a function, this function is the constructor of the class represented, in which all the code can be considered to initialize an object and work. The function used to represent the class is also called a constructor.
In JavaScript, each object can be viewed as a collection of multiple properties (methods)
*/

function Test () {
Alert (typeof (obj));
}




The above code defines a class Class1, this is the simple package in JS, below we see how JS defines "static Class",

function Class1 () {//constructor
}
Static properties
Class1.staticproperty = "Test";
static method
Class1.staticmethod = function () {
alert (Class1.staticproperty);
}

function Test () {
Calling static methods
Class1.staticmethod ();
Alert (typeof (Class1));

}

Then look at the "abstract class":

/*
In traditional object-oriented languages, virtual methods in an abstract class must be declared first, but can be invoked in other methods.
In JavaScript, a virtual method can look at a method that is not defined in the class, but has already been used by the this pointer.
Unlike traditional object-oriented objects, virtual methods are used directly without the need for declaration. These methods will be in the derived class
Implemented in
*/

Defining Extend Methods
Object.extend = function (destination, source) {
For (property in source) {
Destination[property] = Source[property];
}
return destination;
}
Object.prototype.extend = function (object) {
Return Object.extend.apply (this, [this, Object]);
}
Defines an abstract base class base with no constructors
function base () {}
Base.prototype = {
Initialize:function () {
this. OnInit (); A virtual method was invoked
}
}
Define Class1
function Class1 () {
Constructors
}
Let Class1 inherit from base and implement the OnInit method
Class1.prototype = (new base ()). Extend ({
Oninit:function () {//implementation of OnInit virtual methods in abstract base classes
The realization of OnInit function
}
});

As we see above, "let Class1 inherit from base and implement the OnInit method," we use the concept of "inheritance", please note. Let's look at the effect of execution:

function Test () {
var obj = new Class1 ();
Obj.oninit = function () {alert ("Test");}
Obj.oninit ();
}

Related Article

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.