This paper analyzes the definition and usage of JS class. Share to everyone for your reference, specific as follows:
JS can define its own class
It's very interesting.
<script type= "Text/javascript" >
var anim = function () {
alert (' Nihao ');
Anim.prototype.start = function () {
alert (' Start ');
Anim.prototype.stop = function () {
alert (' Stop ');
var Myanim = new Anim ();
Myanim.start ();
Myanim.stop ();
</script>
Anim is a class that pops up Nihao when initialized.
It has two methods, one is the start method and one is the Stop method.
Use the ' point ' to call directly.
<script type= "Text/javascript" >
var anim = function () {
alert (' Nihao ');
Anim.prototype = {
start:function () {
alert (' Start ');
},
stop:function () {
alert (' Stop ');
}
};
var Myanim = new Anim ();
Myanim.start ();
Myanim.stop ();
</script>
Another way of defining the same effect as above.
The third type,
<script type= "Text/javascript" >
var anim = function () {
alert (' Nihao ');
Function.prototype.method = function (name, FN) {//This very useful
this.prototype[name] = fn;
Anim.method (' Start ', function () {
alert (' Start ');
});
Anim.method (' Stop ', function () {
alert (' Stop ');
});
var Myanim = new Anim ();
Myanim.start ();
Myanim.stop ();
</script>
For more on JavaScript related content to view the site topics: "JavaScript object-oriented Tutorial", "javascript json operation tips Summary", "JavaScript switching effects and techniques summary", " JavaScript Search Algorithm Skills Summary, "JavaScript animation effects and techniques Summary", "JavaScript Error and debugging skills Summary", "JavaScript data structure and algorithm skills summary", "JavaScript traversal algorithm and skills summary" and The summary of JavaScript mathematical operational usage
I hope this article will help you with JavaScript programming.