Home > Web Front-end > JS Tutorial > JS object-oriented programming object usage analysis_javascript skills

JS object-oriented programming object usage analysis_javascript skills

WBOY
Release: 2016-05-16 18:21:13
Original
806 people have browsed it

Because everyone always uses process-oriented programming ideas to write JS code, and because the Internet is full of too many small "clever" JS code snippets, many of which are random and very irregular, this has caused everyone to The "misunderstanding" of JS is that it is just a small auxiliary tool and is not suitable for the development of large things. However, since the rise of AJAX, writing a large amount of JS code requires people to be able to develop object-oriented development just like writing JAVA-like code.

So below, I will combine my own experience and what I have learned with you to learn how to use object-oriented programming in JS. In fact, it is not difficult to use JS for object-oriented development, because each function in JS is an object, such as the following function:

Copy code The code is as follows:

function HelloWorld()
{
alert('hello world!');
}

Then we can use it as an object when using it, such as using the following test function:
Copy code The code is as follows:

function _test()
{
var obj = new HelloWorld();
}

Then call the _test method Then hello world will pop up! prompt box, that is, the HelloWorld() object (function) is called. The HelloWorld object here does not have any properties or methods. It has only one construction method HelloWorld(). We can think of it as a class in JAVA without any properties and methods. When using new to create an object, it is called Its construction method. This is also our simplest object. Of course, an object must be assigned attributes and methods. In JS, we use the prototype keyword to assign values. For example, I want to add a sayHello method and a name attribute to the HelloWorld object. , then you can add it like this:
Copy code The code is as follows:

HelloWorld.prototype = {
name : 'JavaScript',
sayHello : function() {
alert(this.name);
}
}

Then it can be HelloWorld Added a name attribute and sayHello method, let's change the _test method as follows:
Copy code The code is as follows:

function _test()
{
var obj = new HelloWorld();
obj.sayHello();
}

Then call After the _test method, hello wordl! and JavaScript will be printed successively (one is the alert in the construction method, and the other is the alert in the sayHello method). Note that the alert in the sayHello method refers to the this keyword, which represents the HelloWorld object and points to this object by default, just like the this keyword in JAVA.

To add instance methods and properties to an object, we can use the above method, that is, use the prototype keyword to assign values. The format is as follows:

Object name.prototype = {
Properties One: attribute value,
Attribute two: attribute value,
Method one: function (parameter list) {
Method body;
},
Method two: function (parameter list) {
Method body;
}
}

You can define multiple properties and methods for an object as above, so that after new an object, you can use the instance name.property or Method to get properties or execute methods.

In the above method, you may not know that no object's attributes can be directly accessed. For example, to access the name attribute of the HelloWorld object, you can use obj.name to obtain it directly. This is like the public attributes in our JAVA, and we can also directly assign values ​​to the name attribute. So now there is a question, how do we assign a private member variable to an object? Then we may have to change the way of declaring the HelloWorld class. Instead of using prototype to declare the attributes and methods of the class, we directly use inline functions and attributes to declare. The modified HelloWorld is as follows, we named it HelloWorld2:
Copy code The code is as follows:

function HelloWorld2()
{
var privateProp = ' hello world 2!';
this.method = function() {
alert(privateProp);
}
}

Have you seen the class declaration method of HelloWorld2? The function nested declaration is made directly inside the function, and we also set a local variable privateProp, which is our private member variable. This variable can only be accessed by functions inside HelloWorld2. External access is not allowed, so we You can cleverly set private variables of a class by using the scope of the variable. Our application is as follows:
Copy code The code is as follows:

function _test2()
{
var obj2 = new HelloWorld2();
obj2.method(); // Calling this method will print 'hello world 2!
alert(obj2.privateProp); // Will print undefined
}

The above are all about how to define a class, and how to define attributes and methods for a class. Since the definition is clear and clear using the prototype method, this method is generally used to define the class. Definition, and similar class declaration methods are now used in many AJAX frameworks. Moreover, the private member variables of the class can only be accessed by functions in the construction method of the class. In this way, the methods declared by the prototype of the class cannot access the private member variables, and the readability is not as good as the prototype method.

Okay, the above is all about defining instance methods and attributes of a class. In JAVA, classes are divided into instance methods and properties and class methods and properties. The so-called class attributes and methods mean that all instances of the class only maintain a copy of the class attributes and class methods, instead of maintaining a set for each instance. This is different from instance attributes and instance methods. So how to define static class methods and class attributes for a class in JS? We can directly add static attributes and static methods to the class. For example, add an age static attribute and a hello static method to the HelloWorld class, then the declaration is as follows:
Copy code The code is as follows:

HelloWorld.age = 22;
HelloWorld.hello = function() {
alert(HelloWorld.age);
}

Then the static attribute age and the static method hello are declared for the class HelloWorld. When using it, you can directly use the class name to access, but you cannot use the instance to access. This is consistent with JAVA. The test is as follows:
Copy code The code is as follows:

function _test()
{
var obj = new HelloWorld();
obj.sayHello(); / / Correct, instance method, can be accessed through the instance
HelloWorld.hello(); // Correct, static method, directly accessed through the class name
obj.hello(); // Error, cannot be accessed through the instance static method. A JS error will be reported!
}

Through the above explanation, I believe that everyone has a certain understanding of object-oriented programming with JS, and they must be ready to start. Haha, you might as well give it a try~~ (Note : All the above codes passed the test!)
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template