This article describes the generation method of JS anonymous function class. Share it for your reference, as follows:
<script type="text/javascript"> var Book = (function() { // Private static properties var numOfBooks = 0; // Private static method function checkIsbn(isbn) { if(isbn == undefined || typeof isbn != 'string') { return false; } return true; } // Return the constructor return function(newIsbn, newTitle, newAuthor) { // implements Publication // Private attributes var isbn, title, author; // Privileged method = function() { return isbn; }; = function(newIsbn) { if(!checkIsbn(newIsbn)) throw new Error('Book: Invalid ISBN.'); isbn = newIsbn; }; = function() { return title; }; = function(newTitle) { title = newTitle || 'No title specified'; }; = function() { return author; }; = function(newAuthor) { author = newAuthor || 'No author specified'; }; // Control the number of objects and construct the function numOfBooks++; // 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.'); (newIsbn); (newTitle); (newAuthor); } })(); // Public static method = function(inputString) { alert('convertToTitleCase'); }; // Public non-privileged method = { display: function() { alert("isbn:"+()+" title:"+()+" author:"+()); } }; //var the Hobbit = new Book(123, '', 'J. R. R. Tolkien'); // Non-string throws an exceptionvar theHobbit = new Book('1990-78sd-1092', '', 'J. R. R. Tolkein'); (); //(); // Uncaught TypeError: Object #<Object> has no method 'convertToTitleCase' (); // Output convertToTitleCasevar theHobbit2 = new Book('1990-78sd-1092', '', 'J. R. R. Tolkein'); (); var theHobbit3 = new Book('1990-78sd-1092', '', 'J. R. R. Tolkein'); (); var theHobbit4 = new Book('1990-78sd-1092', '', 'J. R. R. Tolkein'); (); var theHobbit5 = new Book('1990-78sd-1092', '', 'J. R. R. Tolkein'); (); var theHobbit6 = new Book('1990-78sd-1092', '', 'J. R. R. Tolkein'); (); // Uncaught Error: Book: Only 5 instances of Book can be created. </script>
Here, I have made JS superb, admired it to the extreme, the code is clear and concise, beautiful, and the annotations are just right.
For more information about JavaScript, please view the special topic of this site: "Summary of common JavaScript functions techniques》、《JavaScript object-oriented tutorial》、《Summary of json operation skills in JavaScript》、《Summary of JavaScript switching effects and techniques》、《Summary of JavaScript search algorithm skills》、《Summary of JavaScript animation effects and techniques》、《Summary of JavaScript Errors and Debugging Skills》、《Summary of JavaScript data structure and algorithm techniques》、《Summary of JavaScript traversal algorithm and skills"and"Summary of JavaScript mathematical operations usage》
I hope this article will be helpful to everyone's JavaScript programming.