Home  >  Article  >  Web Front-end  >  javascript object-oriented JavaScript class_js object-oriented

javascript object-oriented JavaScript class_js object-oriented

WBOY
WBOYOriginal
2016-05-16 18:28:14960browse

In the previous section JavaScript Object-Oriented Namespace we talked about how to define JavaScript namespace, this section will talk about the next concept - class. Although there is no class keyword in JavaScript, as developers we must have this idea. In C#, classes can be divided into instance classes and static classes, and the same is true in JavaScript.


1. Define the instance class: In the previous section, I defined a namespace of cnblogs.news. Now I define a class named Article under this namespace:

Copy code The code is as follows:

cnblogs.news.Article=function(){
var _this=this;
this.title=null;
this.content=null;
this.show=function(){
document.write("

" _this.title "document.write("

" _this.content "

");
}
}


Creating an object is just like Same as C#:
Copy code The code is as follows:

// Instantiate an object
var article =new cnblogs.news.Article();
// Assign values ​​to the properties of the object
article.title="This is the title of the article";
article.content="This is the content of the article";
// Call the method of the object
article.show();

Second, define the static class: The so-called static class is to directly call the members of the class. In other words, the members of the class belong to Class, not an object. Taking Article as an example, the code is as follows:
Copy code The code is as follows:

cnblogs.news .Article={
title: "This is the title of the article",
content: "This is the content of the article",
show:function(){
document.write("

" cnblogs.news.Article.title "

");
document.write("

" cnblogs.news.Article.content "

");
}
};

The calling method is also similar to C#:
cnblogs.news.Article.show();
Perhaps you have discovered by now that the so-called JavaScript static class is actually A json object, congratulations, you got the answer right! ^_^
Three, how to choose:
So when to choose instance class and when to choose static class? As far as personal experience is concerned (please correct me if I am wrong, it can be corrected in any way^_^ ), develop some programs that are weakly dependent on DOM but require strong reusability, such as tool classes, plug-in classes, structures, and use static classes; on the other hand, if the program has a strong dependence on DOM, variables are often passed around. , or cause changes to the structure of the class, then use the instance class. Personally, I prefer the first solution. Its coding style is more like C# than the second one. I think students who are used to writing C# will feel the same way, ^_^.
Author: Uncle Xiang
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