Implementation of Javascript classes

Source: Internet
Author: User
It is often seen in several groups that someone asks how a function in a class calls this. The exposed method after definition. Now I am publishing an article on implementation of the class. First, let's talk about the class. In a class, we will have the following features: 1. Public method 2. Private method 3. Attribute 4. Private... SyntaxHighlighter. all ();

It is often seen in several groups that someone asks how a function in a class calls this. The exposed method after definition. Now I am publishing an article on implementation of the class.
First, let's talk about the class. In a class, we have the following features:
1. Public Method
2. Private Method
3. Attributes
4. Private Variables
5. Constructor
Let's look at an example:
/*** Define class ***/
Var Class = function (){
Var _ self = this; // reference a negative value to a variable.
 
Var _ Field = "Test Field"; // Private Field
Var privateMethod = function () {// Private Method
Alert (_ self. Property); // call the Property
}
 
This. Property = "Test Property"; // public Property
This. Method = function () {// public Method
Alert (_ Field); // call a private Field
PrivateMethod (); // call the private Method
}
}
  
I have written all the comments here, and you will probably understand them at a glance. For friends who write less JavaScript code, they may wonder why I define a variable named _ self, because in JS, this does not need to be used in other object languages, this will change in the parsing and running processes. Here is a brief introduction to the definition of this in js. If you need this, I can open one more article.
Definition: this is the object to which the function that contains it is called as a method.
Feature: the environment of this can be changed as the function is assigned to different objects!
If you are interested, you can find information on the Internet to find out the correct answer. Here _ self aims to open a private variable and direct the reference to the class itself. Www.2cto.com
I have just mentioned a constructor problem, which can be implemented directly using code. At the end of the function, write and execute the Code directly.
/*** Define class ***/
Var Class = function (){
Var _ self = this; // reference a negative value to a variable.
 
Var _ Field = "Test Field"; // Private Field
Var privateMethod = function () {// Private Method
Alert (_ self. Property); // call the Property
}
 
This. Property = "Test Property"; // public Property
This. Method = function () {// public Method
Alert (_ Field); // call a private Field
PrivateMethod (); // call the private Method
}
 
/*** Constructor ***/
Var init = function (){
PrivateMethod ();
}
Init ();
}
  
Use this class
Var c = new Class ();
C. Method (); // usage
So OK.

 

From the heart and white
 
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.