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>
js深入研究之初始化验证的更多相关文章
- 使用jquery.validate.js实现boostrap3的校验和验证
使用jquery.validate.js实现boostrap3的校验和验证 boostrap3验证框架 jquery.validate.js校验表单 >>>>>>& ...
- js/jquery/插件表单验证
媳妇要学js,就收集一些资料给她. 1.js 表单验证 : http://hi.baidu.com/yanchao0901/item/161f563fb84ea5433075a1eb 2.jquery ...
- 关于vue.js element ui 表单验证 this.$refs[formName].validate()的问题
方法使用前需了解: 来自”和“小编的小提示: 首先打印一下this.$refs[formName],检查是否拿到了正确的需要验证的form. 其次在拿到了正确的form后,检查该form上添加 ...
- js和jquery页面初始化加载函数的方法及先后顺序
运行下面代码.弹出A.B.C.D.E的顺序:A=B=C>D=E. jquery:等待页面加载完数据,以及页面部分元素(不包括图片.视频), js:是页面全部加载完成才执行初始化加载. <! ...
- jQuery/js 正则收集(邮件验证、)
var reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/; //验证邮箱的正则表达式if( ...
- 用正则表达式在注册页面(js/aspx.cs)的验证
1.验证邮箱(用户名) JS页面中: 首先定义变量和正则 var usermail = $("#usermail" ).val(); var username= /^([a-zA- ...
- js函数、表单验证
惊天bug!!!在script里面只要有一点点错误,就都不执行了!!!所以每写一个方法,就跑一下,因为这个书写疏忽导致的bug不可估量!!! [笑哭,所以我才这么讨厌js么,后来真心的是一点都不想再看 ...
- js深入研究之Person类案例
<script type="text/javascript"> /* 定义一个Person类 */ function Person(name, age) { this. ...
- js深入研究之类定义与使用
js可以定义自己的类 很有意思 <script type="text/javascript"> var Anim = function() { alert('nihao ...
随机推荐
- javascript算法挑战
1.翻转字符串算法挑战: 先把字符串转化成数组,再借助数组的reverse方法翻转数组顺序,最后把数组转化成字符串. 你的结果必须得是一个字符串 function reverseString(str) ...
- Linux红黑树(一)——数据结构
摘要 兹博文探讨四个重点:1.简单介绍红黑树:2.红黑树节点数据结构:3.红黑树节点中父节点指针域和自身节点颜色有机结合:4.定义红黑树和操作树节点父节点指针和节点颜色的接口,包括一系列宏和两个函数. ...
- POJ 2449 Remmarguts' Date (SPFA + A星算法) - from lanshui_Yang
题目大意:给你一个有向图,并给你三个数s.t 和 k ,让你求从点 s 到 点 t 的第 k 短的路径.如果第 k 短路不存在,则输出“-1” ,否则,输出第 k 短路的长度. 解题思路:这道题是一道 ...
- input事件以及中文输入法的处理
在项目的开发过程中,相信大家都处理过监听用户输入的事情,一般我们会用到onkeyup.onkeydown.onkeypress.onchange.oninput事件,虽然都很熟悉了,但是还是有必要巩固 ...
- ReportViewer导出功能筛选
ReportViewer只能导出Excel,把导出Word和PDF功能去掉 <rsweb:ReportViewer ID="ReportViewer1" runat=&quo ...
- Fiddler使用笔记
http://www.cnblogs.com/TankXiao/archive/2012/02/06/2337728.html#basic 1.filter的使用,跟踪某个网站的访问,例如:hr. ...
- openfire spark 二次 开发 服务插件
==================== 废话 begin ============================ 最近老大让我为研发平台增加即时通讯功能.告诉我用comet 在web端实现即 ...
- Linux 开机报 or type Control-D to continue
解决步骤: 1.输入root密码 2.看是哪个盘报的错,我这边是sda3(可能会是不同的盘),就是代码中标为FAIL 输入以下命令fsck -y /dev/sda3
- Ubuntu中安装编译并测试HTK语音识别库
1.在网上看到首先必须确保电脑上安装了g++和libx11 g++ --version //检测g++版本 sudo apt-get install libx11-dev:i386 2.然后可从HTK ...
- 导入表数据 txt
导入表数据 txt mysql> load data infile "D:/import.txt" into table shop;输出: Query OK, rows af ...