Object-oriented JavaScript (2): Class

Source: Internet
Author: User

In small projects for JavaScript use, just write a few function on the line. But in large projects, especially in the development of the pursuit of a good user experience of the site, such as SNS, will use a lot of javascrpt, sometimes JavaScript workload than C #, then write a bunch of function, it will appear very messy, disorganized, even a naming conflict, It's cumbersome to manage and maintain. In this case, we need to use object-oriented thinking to develop JavaScript. So let's just say:

In the previous section, object-oriented JavaScript (1): The namespace says how to define a JavaScript namespace, which is followed by a concept-class. Although there is no class keyword in JavaScript, we must have this idea as a developer. Classes can be divided into instance classes and static classes in C #, as do JavaScript.

First, define the instance class : In the previous section I defined a Cnblogs.news namespace and now define a class named article under this namespace:

Cnblogs.news.article=function () {
var _this=this;
This.title=null;
This.content=null;
This.show=function () {
document.write ("document.write ("<p>" +_this.content+ "</p>");
}
}

Creating objects is the same as C #:

Instantiate an Object
var article =new cnblogs.news.Article ();
Assigning values to an object's properties
Article.title= "This is the title of the article";
Article.content= "This is the content of the article";
Methods for calling Objects
Article.show ();


Two, define static classes : The so-called static class is called directly to the members of the class, in other words, the members of the class belong to the class, not the object. Also take article as an example, the code is as follows:

cnblogs.news.article={
Title: "This is the title of the article",
Content: "This is the contents of the article",
Show:function () {
document.write ("document.write ("<p>" +cnblogs.news.article.content+ "</p>");
}
};

They are also called in the same way as C #:

Cnblogs.news.Article.show ();

Here you may have found that the so-called JavaScript static class is actually a JSON object, congratulations, correct! ^_^

three, how to choose :
Then when to choose the instance class, when to choose static class, in terms of personal experience (say not treatise Dao, how can be ^_^), to develop some of the DOM depends on the weak, and requires a strong reuse of the program, such as tool class, plug-in class, structure, use static class; Conversely, if the program is highly dependent on DOM, There are often variables to pass through, or to change the structure of the class, then choose the instance class. Personal comparison of the first program, its code style is more like C # than the second, I would like to write the C # students will feel the same, ^_^.

Object-oriented JavaScript (2): Class

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.