您好,欢迎访问一九零五行业门户网

js初始化验证实例详解

本文实例讲述了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进行验证。是否定义,是否为字符串等等。对title进行判断,设置默认。
另一种实现方式
<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>
其它类似信息

推荐信息