An example of the generation of JS anonymous function class _javascript skills

Source: Internet
Author: User

This example describes the generation of JS anonymous function classes. Share to everyone for your reference, specific as follows:

<script type= "Text/javascript" > var book = (function () {//private static property var numofbooks = 0;
  private static Method function Checkisbn (ISBN) {if (ISBN = undefined | | | typeof ISBN!= ' string ') {return false;
 return true; //Returns constructor return function (NEWISBN, Newtitle, Newauthor) {//Implements publication//private Property Var ISBN, title, Autho
  R
  Privileged Method THIS.GETISBN = function () {return ISBN;
  };
   THIS.SETISBN = function (NEWISBN) {if (!CHECKISBN (NEWISBN)) throw new Error (' Book:invalid ISBN. ');
  ISBN = NEWISBN;
  };
  This.gettitle = function () {return title;
  }; This.settitle = function (newtitle) {title = Newtitle | |
  ' No title specified ';
  };
  This.getauthor = function () {return author;
  }; This.setauthor = function (newauthor) {author = Newauthor | |
  ' No author specified ';
  }; The number of control objects and the numofbooks++ of constructors;
  Keep track of how many books have been instantiated//with the private static attribute. if (Numofbooks > 5) throw new ERror (' book:only 5 instances of book can be ' + ' created. ');
  THIS.SETISBN (NEWISBN);
  This.settitle (Newtitle);
 This.setauthor (Newauthor);
}
})();
public static Method book.converttotitlecase = function (inputstring) {alert (' converttotitlecase '); Public non-privileged method Book.prototype = {display:function () {Alert ("ISBN:" +this.getisbn () + "title:" +this.gettitle () + "Author:" +t
 His.getauthor ());
}
}; var thehobbit = new book (123, ', ' J. R. R. Tolkein ');
Non-string throw exception var thehobbit = new book (' 1990-78sd-1092 ', ', ', ' J. R. R. Tolkein ');
Thehobbit.display (); Thehobbit.converttotitlecase (); Uncaught Typeerror:object #<object> has no method ' Converttotitlecase ' book.converttotitlecase ();
Output converttotitlecase var theHobbit2 = new book (' 1990-78sd-1092 ', ', ', ' J. R. R. Tolkein ');
Thehobbit2.display ();
var theHobbit3 = new book (' 1990-78sd-1092 ', ', ' J. R. R. Tolkein ');
Thehobbit3.display ();
var theHobbit4 = new book (' 1990-78sd-1092 ', ', ' J. R. R. Tolkein ');
Thehobbit4.display (); var ThehoBbit5 = new book (' 1990-78sd-1092 ', ', ', ' J. R. R. Tolkein ');
Thehobbit5.display ();
var theHobbit6 = new book (' 1990-78sd-1092 ', ', ' J. R. R. Tolkein '); Thehobbit6.display ();
Uncaught Error:Book:Only 5 Instances of book can be created.

 </script>

Here has been the JS superb, admiration to the extreme, the code clear and concise, beautiful, the annotation just right.

For more on JavaScript related content to view the site topics: "JavaScript common function Tips Summary", "JavaScript object-oriented tutorial", "JavaScript in the JSON Operation tips Summary", " JavaScript switching effects and techniques summary, "JavaScript Search Algorithm Skills Summary", "JavaScript animation effects and Skills summary", "JavaScript Error and debugging skills Summary", "JavaScript data structure and algorithm skills summary", JavaScript traversal algorithm and techniques summary and JavaScript mathematical Operational Usage Summary

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.