<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. Windows下重启指定名称的服务

    // 重启指定服务 void CPSSDPrinterCtrlPlug::RestartService(const wchar_t* nswServiceName) { SC_HANDLE schSC ...

  2. java笔记5之逻辑运算符以及&&与&的区别

    1 &逻辑与:有false则false.         |逻辑或:有true则true.         ^逻辑异或:相同为false,不同为true.            举例:情侣关系 ...

  3. Eclipse Tomcat : Project facet Java version 1.7 is not supported.

    在Eclipse打开一个项目,并用Tomcat运行时,报错:Project facet Java version 1.7 is not supported. 大致的截图如下: "项目中的jd ...

  4. Android下如何理解onMeasure,onLayout的过程

    在Android中view如何完成绘制这个过程介绍了很多,但是很多理论化的东西,最近重新整理一下,通俗的讲解一下. View绘制过程就好比你向银行贷款, 在执行onMeasure的时候,好比银行告诉你 ...

  5. thread block grid

    grid里面包含block,block里面包含thread grid里面所有的block都是同样大小的,  每个block最多可以有1024个thread. blockDim表示一个block里面th ...

  6. Vim 自动文件头注释与模板定义

    Vim 自动文件头注释与模板定义 在vim的配置文件.vimrc添加一些配置可以实现创建新文件时自动添加文件头注释,输入特定命令可以生成模板. 使用方法 插入模式输入模式输入seqlogic[Ente ...

  7. 免费SVN源代码在线托管

    免费的SVN源代码在线托管网站很多,用的最多的是TaoCode吧.但是一般都要求开源,支持私有项目的普遍收费,要不就是流量很少,不够用.对比了一下,发现好库正好能满足需要. 网址: http://ww ...

  8. Chrome: Shockwave Flash isn't responding

    这个问题问 Google 解决得比较快,百度里尽是转载党的东西! 1. 到 chrome://settings/content 找到 Plug-ins 项目 2. 点击 Plug-ins 里的 Exc ...

  9. mysql 分区表详解

    项目中要一张库表实现 list分区.并且支持多种数据库. oracle 很顺利,只是mysql 听说5.1版本就已经支持了, 可是试了很多个版本,都不行,后来查到原因是要5.5 以上版本 分区才支持 ...

  10. Swift 流程控制

    import Foundation ...{ == { print(index) } } // 可选变量 类型后面加? var myName:String?="jikexueyuan&quo ...