js初始化验证实例详解
本文实例讲述了js初始化验证的方法。分享给大家供大家参考,具体如下:
<scripttype="text/javascript"> varBook=function(isbn,title,author){ if(!this.checkIsbn(isbn)){ thrownewError('Book:InvalidISBN.'); } this.isbn=isbn; this.title=title||'Notitlespecified'; this.author=author||'Noauthorspecified'; } Book.prototype={ checkIsbn:function(isbn){ if(isbn==undefined||typeofisbn!='string'){ returnfalse; } returntrue;//Alltestspassed. }, display:function(){ alert("isbn:"+this.isbn+"title:"+this.title+"author:"+this.author); } }; vartheHobbit=newBook('0-395-07122-4','TheHobbit','J.R.R.Tolkein'); theHobbit.display();//OutputsthedatabycreatingandpopulatinganHTMLelement. </script>
对isbn进行验证。是否定义,是否为字符串等等。对title进行判断,设置默认。
另一种实现方式
<scripttype="text/javascript"> /*出版interface.*/ /*varPublication=newInterface('Publication',['getIsbn','setIsbn','getTitle', 'setTitle','getAuthor','setAuthor','display']);*/ varBook=function(isbn,title,author){//implementsPublication this.setIsbn(isbn); this.setTitle(title); this.setAuthor(author); } Book.prototype={ checkIsbn:function(isbn){ if(isbn==undefined||typeofisbn!='string'){ returnfalse; } returntrue;//Alltestspassed. }, getIsbn:function(){ returnthis.isbn; }, setIsbn:function(isbn){ if(!this.checkIsbn(isbn))thrownewError('Book:InvalidISBN.'); this.isbn=isbn; }, getTitle:function(){ returnthis.title; }, setTitle:function(title){ this.title=title||'Notitlespecified'; }, getAuthor:function(){ returnthis.author; }, setAuthor:function(author){ this.author=author||'Noauthorspecified'; }, display:function(){ alert("isbn:"+this.isbn+"title:"+this.title+"author:"+this.author); } }; vartheHobbit=newBook('0-395-07122-4','','J.R.R.Tolkein'); theHobbit.display();//OutputsthedatabycreatingandpopulatinganHTMLelement. </script>
接口实现,参考接口,定义了好多方法。
内部方法命名加_,例如这个检测的方法_checkIsbn
<scripttype="text/javascript"> /*出版interface.*/ /*varPublication=newInterface('Publication',['getIsbn','setIsbn','getTitle', 'setTitle','getAuthor','setAuthor','display']);*/ varBook=function(isbn,title,author){//implementsPublication this.setIsbn(isbn); this.setTitle(title); this.setAuthor(author); } Book.prototype={ _checkIsbn:function(isbn){ if(isbn==undefined||typeofisbn!='string'){ returnfalse; } returntrue;//Alltestspassed. }, getIsbn:function(){ returnthis.isbn; }, setIsbn:function(isbn){ if(!this._checkIsbn(isbn))thrownewError('Book:InvalidISBN.'); this.isbn=isbn; }, getTitle:function(){ returnthis.title; }, setTitle:function(title){ this.title=title||'Notitlespecified'; }, getAuthor:function(){ returnthis.author; }, setAuthor:function(author){ this.author=author||'Noauthorspecified'; }, display:function(){ alert("isbn:"+this.isbn+"title:"+this.title+"author:"+this.author); } }; //vartheHobbit=newBook(123,'','J.R.R.Tolkein');//非字符串抛出异常 vartheHobbit=newBook('1990-78sd-1092','','J.R.R.Tolkein'); theHobbit.display();//OutputsthedatabycreatingandpopulatinganHTMLelement. </script>
更多关于JavaScript相关内容可查看本站专题:《javascript面向对象入门教程》、《JavaScript中json操作技巧总结》、《JavaScript切换特效与技巧总结》、《JavaScript查找算法技巧总结》、《JavaScript动画特效与技巧汇总》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》
希望本文所述对大家JavaScript程序设计有所帮助。