Javascript Object-Oriented Programming basics: Encapsulation

Source: Internet
Author: User
"One of the core concepts of Object-oriented thinking is class. A class represents the abstraction of a class of things with similar properties. By instantiating a class, you can obtain an instance (that is, an object) that belongs to the class )". For a long time (I am happy to say this here), js is "an embellishment that can accomplish very limited functions, such as form verification, the language itself has been used as a procedural language, making it difficult to complete complex functions. ". But (here I have to be bitter and bitter), "the emergence of Ajax makes complex scripts a necessary component, which puts forward new requirements for JavaScript program design, many Ajax applications begin to develop with the JavaScript object-oriented nature to make the logic clearer. In fact, JavaScript provides a sound mechanism to implement object-oriented development ideas .". I didn't want to learn what I was afraid of. Now I have to learn it hard.
There is so much nonsense about objects here. We all know that object-oriented programming has three main features: encapsulation, inheritance, and polymorphism. Next we will record some learning experiences around these three features.
Okay. Let's start with encapsulation. As we all know, objects are the most basic unit of encapsulation. Encapsulation prevents changes caused by program dependency. Object-oriented encapsulation is clearer and more powerful than traditional language encapsulation. Code is cheap. Let's look at the simple Code:

// Define the function type definition class
Function class1 (){
// Class member definition and constructor
// Here class1 is both a function and a class. As a function, it can be understood as a class constructor responsible for initialization.
}

// Use the new operator to obtain an instance of a class
Var obj = new class1 ();
/* Aside from the concept of classes, class1 is a function in the code form. Can all functions be operated using new? The answer is yes.
In JavaScript, functions and classes are a concept. When a new function is used, an object is returned. If no class member is initialized in this function, an empty object is returned.
In fact, when a new function is used, this function is the constructor representing the class. All the code can be seen as working to initialize an object. Functions used to represent classes are also called constructors.
In JavaScript, each object can be considered as a set of multiple attributes (methods ).
*/

Function test (){
Alert (typeof (obj ));
}




The code above defines a class class1, which is a simple encapsulation in js. Let's take a look at how js defines "static classes ",

Function class1 () {// Constructor
}
// Static attributes
Class1.staticProperty = "test ";
// Static method
Class1.staticMethod = function (){
Alert (class1.staticProperty );
}

Function test (){
// Call the static method
Class1.staticMethod ();
Alert (typeof (class1 ));

}

Next, let's look at the "abstract class ":

/*
In traditional object-oriented languages, Virtual Methods in abstract classes must be declared first, but can be called in other methods.
In JavaScript, the virtual method can be used to check that there is no defined method in the class, but it has been used through the this pointer.
Unlike traditional object-oriented methods, virtual methods are directly used without being declared. These methods will be used in the derived class
Implementation
*/

// Define the extend Method
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]);
}
// Define an abstract base class with no Constructor
Function base (){}
Base. prototype = {
Initialize: function (){
This. oninit (); // a virtual method is called.
}
}
// Define class1
Function class1 (){
// Constructor
}
// Let class1 inherit from the base and implement the oninit Method
Class1.prototype = (new base (). extend ({
Oninit: function () {// implement the oninit virtual method in the abstract base class
// Oninit function implementation
}
});

We can see that the above "Let class1 inherit from the base and implement the oninit method" uses the "inheritance" concept, please note. Let's take a look at the execution results:

Function test (){
Var obj = new class1 ();
Obj. oninit = function () {alert ("test ");}
Obj. oninit ();
}


For more javascript Object-Oriented Programming basics: encapsulate related articles, please follow the PHP Chinese network!

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.