js初始化驗(yàn)證實(shí)例詳解
本文實(shí)例講述了js初始化驗(yàn)證的方法。分享給大家供大家參考,具體如下:
<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>
對(duì)isbn進(jìn)行驗(yàn)證。是否定義,是否為字符串等等。對(duì)title進(jìn)行判斷,設(shè)置默認(rèn)。
另一種實(shí)現(xiàn)方式
<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>
接口實(shí)現(xiàn),參考接口,定義了好多方法。
內(nèi)部方法命名加_,例如這個(gè)檢測(cè)的方法 _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>
更多關(guān)于JavaScript相關(guān)內(nèi)容可查看本站專題:《javascript面向?qū)ο笕腴T教程》、《JavaScript中json操作技巧總結(jié)》、《JavaScript切換特效與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript動(dòng)畫特效與技巧匯總》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
- javascript 定義初始化數(shù)組函數(shù)
- js的onload事件及初始化按鈕事件示例代碼
- jquery跟js初始化加載的多種方法及區(qū)別介紹
- 淺談js中變量初始化
- AngularJS初始化靜態(tài)模板詳解
- javascript利用初始化數(shù)據(jù)裝配模版的實(shí)現(xiàn)代碼
- javascript 手機(jī)號(hào)碼正則表達(dá)式驗(yàn)證函數(shù)
- jquery validate.js表單驗(yàn)證的基本用法入門
- js驗(yàn)證表單大全
- js驗(yàn)證是否為數(shù)字的總結(jié)
- JS驗(yàn)證URL函數(shù) 正則
相關(guān)文章
用JS實(shí)現(xiàn)根據(jù)當(dāng)前時(shí)間隨機(jī)生成流水號(hào)或者訂單號(hào)
本文通過實(shí)例代碼給大家介紹了基于JS實(shí)現(xiàn)根據(jù)當(dāng)前時(shí)間隨機(jī)生成流水號(hào)或者訂單號(hào)的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-05-05
簡(jiǎn)單談?wù)凧S數(shù)組中的indexOf方法
最近在工作中遇到一個(gè)小問題,這篇文章代碼我會(huì)簡(jiǎn)化成小例子展示給大家。給大家詳細(xì)的介紹JS數(shù)組中的indexOf方法,用心看到最后會(huì)有收獲哈,有需要的朋友們下面來一起看看吧。2016-10-10
jQuery右下角旋轉(zhuǎn)環(huán)狀菜單特效代碼
jquery實(shí)現(xiàn)右下角旋轉(zhuǎn)環(huán)形菜單特效代碼,是固定在頁面右下角位置,當(dāng)用戶點(diǎn)擊了主菜單按鈕后,子菜單項(xiàng)會(huì)以環(huán)狀旋轉(zhuǎn)進(jìn)入頁面,并使用animate.css制作動(dòng)畫效果,有需要的朋友可以參考下2015-08-08
JavaScript中關(guān)鍵字 in 的使用方法詳解
這篇文章主要介紹了JavaScript中關(guān)鍵字 in 的使用方法詳解,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10
createTextRange()的使用示例含文本框選中部分文字內(nèi)容
這篇文章主要介紹了createTextRange()的使用示例,需要的朋友可以參考下2014-02-02
微信小程序?qū)崿F(xiàn)手指拖動(dòng)選項(xiàng)排序
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)手指拖動(dòng)選項(xiàng)排序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04
JavaScript編寫帶旋轉(zhuǎn)+線條干擾的驗(yàn)證碼腳本實(shí)例
除了普通的可點(diǎn)擊更換的四位驗(yàn)證碼實(shí)現(xiàn),我們這里還展示了更進(jìn)一步的JavaScript編寫帶旋轉(zhuǎn)+線條干擾的驗(yàn)證碼腳本實(shí)例,需要的朋友可以參考下2016-05-05

