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 ...
随机推荐
- .net连接oracle(无客户端)
使用DDTek.Oracle.dll可以在没有安装oracle客户端的情况下连接远程的oracle. string _conString = "Host=192.168.1.1;Port=1 ...
- ie浏览器中 overflow:hidden无作用的解决方案
原因: overflow:hidden失效 当父元素的直接子元素或者下级子元素的样式拥有position:relative属性时,父元素的overflow:hidden属性就会失效. 我在ie内发现子 ...
- MVC 模板页和布局
我们在以前的Asp.NET课程中已经学习过母版页了,在MVC中WebForm视图使用母版页的方法与以前基本相同. 创建一个项目MvcMasterPageDemo. 添加Home控制器,生成Index视 ...
- 【iOS基础】iOS 网络请求
一.一个HTTP请求的基本要素1.请求URL:客户端通过哪个路径找到服务器 2.请求参数:客户端发送给服务器的数据* 比如登录时需要发送的用户名和密码 3.返回结果:服务器返回给客户端的数据* 一般是 ...
- 从不同层面看cocos2d-x
一 框架层面 二 Lua层面 三 工具层面 四 android打包 一 框架层 总体来说,cocos2dX提供的一个简便的框架,包括了渲染,动画,事件分发,网络还有UI,物理引擎等几大 ...
- NYOJ 16 矩形嵌套(动态规划)
矩形嵌套 时间限制: 3000 ms | 内存限制: 65535 KB 难度: 4 描述 有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形X(a,b)可以嵌套在矩形Y(c,d)中当且仅 ...
- MVC实现类似QQ的网页聊天功能-Ajax(上)
说到QQ聊天,程序员首先想到的就是如何实现长连接,及时的接收并回馈信息.那么首先想到的就是Ajax,Ajax的运行机制是通过XMLHttpRequest向服务器发出异步请求,并接受数据,这样就可以实现 ...
- C#socket通讯两个最经典错误解决方案
1.经典错误之 无法访问已释放的对象. 对象名:“System.Net.Sockets.Socket” (1).问题现场 (2).问题叙述 程序中的某个地方调用到了socket.close ...
- 初学Java ssh之Spring 第二篇
上一篇我们成功搭建好了spring开发的环境,接下来一起看看spring有什么神奇的地方吧(本人也是新手,有不太对的地方希望大神给指出便于修改呢,谢谢大家). 之前听说spring是在对xml文件的应 ...
- [转]Xcode的重构功能
Xcode提供了以下几个重构功能: Rename Extract Create Superclass Move Up Move Down Encapsulate 在菜单栏中的位置如下图: 在代码区里直 ...