The definition and using method of JS class to explore _javascript skills in depth

Source: Internet
Author: User

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.

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.