_javascript techniques for object usage analysis of JS object-oriented programming

Source: Internet
Author: User
Because we always use process-oriented programming ideas to write JS code, but also because the network is flooded with too many small "clever" JS code section, many are random and for, very irregular, this also caused everyone to JS "misunderstanding", blindly think it is an auxiliary small dongdong, and not suitable for big things development. But since the advent of Ajax, a large number of JS code writing requires people to have the same code like writing Java, can be object-oriented development.

So the following on the combination of my own experience and learn the Dongdong and everyone together to learn how to use object-oriented programming in JS. In fact, using JS for object-oriented development is not difficult, because in JS each function is an object, such as the following function:
Copy Code code as follows:

function HelloWorld ()
{
Alert (' Hello world! ');
}

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

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

Then the Hello world! pops up after the _test method is invoked , which is called the HelloWorld () object (the function). Here HelloWorld This object does not have any properties and methods, it has only one construction method HelloWorld (), we can think of it as a class in Java without any attributes and methods, when the object is created with new, the constructor method is called. This is our simplest object, of course, an object is definitely to give it properties and methods, in JS we use the prototype prototype keyword to assign values, such as I want to give HelloWorld object add a SayHello method and a Name property, Then you can add it like this:
Copy Code code as follows:

Helloworld.prototype = {
Name: ' JavaScript ',
Sayhello:function () {
alert (this.name);
}
}

Then you can add a Name property and a SayHello method for HelloWorld, and we'll change the _test method as follows:
Copy Code code as follows:

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

Then the call to the _test method prints the Hello wordl! and JavaScript (one is alert in the constructor method, and one is alert in the SayHello method). Note that alert in the SayHello method refers to the This keyword, which represents the HelloWorld object, which is the default pointing to the object, as in the Java keyword.

For adding instance methods and properties to an object, we can do this by using the prototype keyword to assign the following format:

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

You can define multiple properties and methods of an object as described above, so that after you have a new object, you can use the instance name. Property or method to get the property or execute the method.

In the above method, people do not know that there is no object of the property can be directly accessed, such as access to the HelloWorld object's Name property can use Obj.name direct access. This is like the public property in our Java, and we can also assign the Name property directly. So now there's a question, how do we assign a private member variable to an object? Then we might want to change the declaration of the HelloWorld class, instead of using prototype to declare the properties and methods of the class, but to declare them directly using inline functions and attributes, the modified HelloWorld are as follows, we named HelloWorld2:
Copy Code code as follows:

function HelloWorld2 ()
{
var privateprop = ' Hello World 2! ';
This.method = function () {
alert (Privateprop);
}
}

See HELLOWORLD2 's class statement? is a function nested declaration directly inside the function, and we also set up a local variable privateprop, our private member variable, which can only be accessed by functions within the HelloWorld2, and external access is not allowed. This allows us to skillfully set the private variables of the class by using the scope of the variable. We apply the following:
Copy Code code as follows:

function _test2 ()
{
var obj2 = new HelloWorld2 ();
Obj2.method (); Calling this method will print ' Hello World 2!
alert (Obj2.privateprop); Will print undefined
}

It's all about defining a class, thinking about defining properties and methods for a class, and because it's clearly defined by the prototype approach, it's generally used to define classes, and now many Ajax frameworks use similar class declarations. The private member variable of a class can only be accessed in the function of the class's constructor, such that the prototype declared method of the class cannot access the private member variable, and there is no prototype way to readability.

All right, all that is said is to define instance methods and properties for a class. In Java, classes have instance methods and attributes and class methods and properties. Class properties and methods are all instances of the class that maintain only a copy of class attributes and class methods, rather than maintaining a set of each instance, which is not the same as instance properties and instance methods. So how do you define static class methods and class attributes for a class in JS? We can add static properties and static methods directly to the class, such as adding an age static property to the HelloWorld class and a static method of Hello, and declare the following:
Copy Code code as follows:

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

So this declares the static attribute age and static method for class HelloWorld hello. It is accessed directly using the class name, but cannot be accessed using an instance, which is consistent with Java, and is tested as follows:
Copy Code code as follows:

function _test ()
{
var obj = new HelloWorld ();
Obj.sayhello (); Correct, instance method, can be accessed through an instance
Helloworld.hello (); Correct, static method, direct access through class name
Obj.hello (); Error, the static method cannot be accessed through an instance. will report JS Error!
}

Through the above instructions, I believe we have a certain understanding of JS object-oriented programming, but also must be ready to go, oh, we may wish to try Ah ~ ~ (Note: The above code all passed the test!) )

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.