<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深入研究之初始化验证的更多相关文章

  1. 使用jquery.validate.js实现boostrap3的校验和验证

    使用jquery.validate.js实现boostrap3的校验和验证 boostrap3验证框架 jquery.validate.js校验表单 >>>>>>& ...

  2. js/jquery/插件表单验证

    媳妇要学js,就收集一些资料给她. 1.js 表单验证 : http://hi.baidu.com/yanchao0901/item/161f563fb84ea5433075a1eb 2.jquery ...

  3. 关于vue.js element ui 表单验证 this.$refs[formName].validate()的问题

        方法使用前需了解: 来自”和“小编的小提示: 首先打印一下this.$refs[formName],检查是否拿到了正确的需要验证的form. 其次在拿到了正确的form后,检查该form上添加 ...

  4. js和jquery页面初始化加载函数的方法及先后顺序

    运行下面代码.弹出A.B.C.D.E的顺序:A=B=C>D=E. jquery:等待页面加载完数据,以及页面部分元素(不包括图片.视频), js:是页面全部加载完成才执行初始化加载. <! ...

  5. jQuery/js 正则收集(邮件验证、)

    var reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/; //验证邮箱的正则表达式if( ...

  6. 用正则表达式在注册页面(js/aspx.cs)的验证

    1.验证邮箱(用户名) JS页面中: 首先定义变量和正则 var usermail = $("#usermail" ).val(); var username= /^([a-zA- ...

  7. js函数、表单验证

    惊天bug!!!在script里面只要有一点点错误,就都不执行了!!!所以每写一个方法,就跑一下,因为这个书写疏忽导致的bug不可估量!!! [笑哭,所以我才这么讨厌js么,后来真心的是一点都不想再看 ...

  8. js深入研究之Person类案例

    <script type="text/javascript"> /* 定义一个Person类 */ function Person(name, age) { this. ...

  9. js深入研究之类定义与使用

    js可以定义自己的类 很有意思 <script type="text/javascript"> var Anim = function() { alert('nihao ...

随机推荐

  1. POJ 3009 深度优先搜索

    问题:打冰球.冰球可以往上下左右4个方向走,只有当冰球撞到墙时才会停下来,而墙会消失.当冰球紧贴墙时,不能将冰球往那个方向打.冰球出界就当输,超过10次还没将冰球打到目标位置也当输.求用最小次数将冰球 ...

  2. 关于Python中的for循环控制语句

    #第一个:求 50 - 100 之间的质数 import mathfor i in range(50, 100 + 1):    for j in range(2, int(math.sqrt(i)) ...

  3. POJ 2886 Who Gets the Most Candies? 线段树。。还有方向感

    这道题不仅仅是在考察线段树,还他妹的在考察一个人的方向感.... 和线段树有关的那几个函数写了一遍就对了,连改都没改,一直在转圈的问题的出错.... 题意:从第K个同学开始,若K的数字为正 则往右转, ...

  4. iOS - 排序的队列中插入数值

    http://stackoverflow.com/questions/8180115/nsmutablearray-add-object-with-order 用Selector http://sta ...

  5. React Native 初识

    Facebook 在 React.js Conf 2015 大会上推出了基于 JavaScript 的开源框架 React Native,本中文教程翻译自 React Native 官方文档. Rea ...

  6. Javascript:DOM动态创建元素实例应用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. openfire连接登陆优化方案

    client登陆openfire,大概总共须要9个来回才完毕登录. 在2G情况下.就表现为client登录特别慢,所以,为解决问题,对openfire进行了例如以下优化 openfire的连接.登陆过 ...

  8. SMO启发式选择

    %% % svm 简单算法设计 --启发式选择 %% clc clear close all % step=0.05;error=1.2; % [data, label]=generate_sampl ...

  9. [Angular 2] Async Http

    Async Pipe: The Asynce pipe receive a Promise or Observable as  input and subscribes to the input, e ...

  10. Qt之加密算法

          在写这篇文章之前,我曾反复思量关于加密的叫法是否准确,更为严格来说,应该是密码散列-将数据(如中英文字母.特殊字符)通过复杂的算法转换为另一种固定长度的值.   QCryptographi ...