Home >Web Front-end >JS Tutorial >Implementation of Javascript classes and static classes_js object-oriented

Implementation of Javascript classes and static classes_js object-oriented

WBOY
WBOYOriginal
2016-05-16 18:30:48984browse

What I want to talk about today is how to write classes and static classes in Javascript. This is my usual method. You can also have a more convenient one, or you can leave a comment to communicate with everyone.
First let’s talk about classes. In a class we will have the following characteristics:
1. Public methods
2. Private methods
3. Attributes
4. Private variables
5. Destructor
Let’s look at an example directly:
Class example

Copy code The code is as follows:

/***define class***/
var Class = function(){
var _self = this;//Add a negative value to a variable
var _Field = "Test Field"; //Private field
var privateMethod = function(){ //Private method
alert(_self.Property); //Call property
}
this.Property = " Test Property"; //Public property
this.Method = function(){ //Public method
alert(_Field); //Call private field
privateMethod(); //Call private method
}
}

I have written all the notes here, and everyone will probably understand them at a glance. For friends who rarely write JS, you may wonder why I define a _self variable, because in JS, this does not need to be used in other object languages, and this will change during its parsing and running processes. Here I will briefly talk about the definition of this in js. I can write more if necessary.
Definition: this is the object to which the function containing it belongs when it is called as a method.
Feature: The environment of this can change as the function is assigned to different objects!
Interested friends can search for information online to learn more. Back to the topic, the purpose of _self here is to open an additional private variable, pointing directly to the class itself.
I just mentioned a destructor issue, which can be implemented directly using code. Just write the execution code directly at the end of the function.
Code
Copy code The code is as follows:

/***define class***/
var Class = function(){
var _self = this;//Put the negative value of self reference to a variable
var _Field = "Test Field"; //Private field
var privateMethod = function(){ //Private method
alert(_self.Property); //Call property
}
this.Property = "Test Property"; //Public property
this.Method = function (){ //Public method
alert(_Field); //Call private field
privateMethod(); //Call private method
}
/***Destructor***/
var init = function(){
privateMethod();
}
init();
}

Use this class, to quote my colleague "It's very simple!"
var c = new Class();
That's OK
That's it for the definition of classes, static classes, we'll have to wait until next time. Because a girl asked me to go drink tea
How far a person can go depends on who he travels with
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn