この記事の例では、jsの初期化検証方法を説明します。参考のために皆さんと共有してください。詳細は次のとおりです:
<script type="text/javascript"> var Book = function(isbn, title, author) { if(!this.checkIsbn(isbn)){ throw new Error('Book: Invalid ISBN.'); } this.isbn = isbn; this.title = title || 'No title specified'; this.author = author || 'No author specified'; } Book.prototype = { checkIsbn: function(isbn) { if(isbn == undefined || typeof isbn != 'string') { return false; } return true; // All tests passed. }, display: function() { alert("isbn:"+this.isbn+" title:"+this.title+" author:"+this.author); } }; var theHobbit = new Book('0-395-07122-4', 'The Hobbit', 'J. R. R. Tolkein'); theHobbit.display(); // Outputs the data by creating and populating an HTML element. </script>
isbn を確認してください。定義されているかどうか、文字列であるかどうかなど。タイトルを判断してデフォルトを設定します。
別の実装方法
<script type="text/javascript"> /* 出版 interface. */ /* var Publication = new Interface('Publication', ['getIsbn', 'setIsbn', 'getTitle', 'setTitle', 'getAuthor', 'setAuthor', 'display']); */ var Book = function(isbn, title, author) { // implements Publication this.setIsbn(isbn); this.setTitle(title); this.setAuthor(author); } Book.prototype = { checkIsbn: function(isbn) { if(isbn == undefined || typeof isbn != 'string') { return false; } return true; // All tests passed. }, getIsbn: function() { return this.isbn; }, setIsbn: function(isbn) { if(!this.checkIsbn(isbn)) throw new Error('Book: Invalid ISBN.'); this.isbn = isbn; }, getTitle: function() { return this.title; }, setTitle: function(title) { this.title = title || 'No title specified'; }, getAuthor: function() { return this.author; }, setAuthor: function(author) { this.author = author || 'No author specified'; }, display: function() { alert("isbn:"+this.isbn+" title:"+this.title+" author:"+this.author); } }; var theHobbit = new Book('0-395-07122-4', '', 'J. R. R. Tolkein'); theHobbit.display(); // Outputs the data by creating and populating an HTML element. </script>
インターフェースの実装、参照インターフェースでは、多くのメソッドが定義されています。
内部メソッド名には _ が追加されます。たとえば、この検出メソッドは _checkIsbn
<script type="text/javascript"> /* 出版 interface. */ /* var Publication = new Interface('Publication', ['getIsbn', 'setIsbn', 'getTitle', 'setTitle', 'getAuthor', 'setAuthor', 'display']); */ var Book = function(isbn, title, author) { // implements Publication this.setIsbn(isbn); this.setTitle(title); this.setAuthor(author); } Book.prototype = { _checkIsbn: function(isbn) { if(isbn == undefined || typeof isbn != 'string') { return false; } return true; // All tests passed. }, getIsbn: function() { return this.isbn; }, setIsbn: function(isbn) { if(!this._checkIsbn(isbn)) throw new Error('Book: Invalid ISBN.'); this.isbn = isbn; }, getTitle: function() { return this.title; }, setTitle: function(title) { this.title = title || 'No title specified'; }, getAuthor: function() { return this.author; }, setAuthor: function(author) { this.author = author || 'No author specified'; }, display: function() { alert("isbn:"+this.isbn+" title:"+this.title+" author:"+this.author); } }; //var theHobbit = new Book(123, '', 'J. R. R. Tolkein'); // 非字符串抛出异常 var theHobbit = new Book('1990-78sd-1092', '', 'J. R. R. Tolkein'); theHobbit.display(); // Outputs the data by creating and populating an HTML element. </script>