欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js初始化驗證實例詳解

 更新時間:2016年11月26日 10:57:49   作者:牛逼的霍嘯林  
這篇文章主要介紹了js初始化驗證,結(jié)合實例形式分析了javascript初始化驗證相關(guān)的判斷、設(shè)置、檢測等相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了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進(jìn)行驗證。是否定義,是否為字符串等等。對title進(jìn)行判斷,設(shè)置默認(rèn)。

另一種實現(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>

接口實現(xiàn),參考接口,定義了好多方法。

內(nèi)部方法命名加_,例如這個檢測的方法 _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動畫特效與技巧匯總》、《JavaScript錯誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運算用法總結(jié)

希望本文所述對大家JavaScript程序設(shè)計有所幫助。

相關(guān)文章

  • 圖片自動縮小的js代碼,用以防止圖片撐破頁面

    圖片自動縮小的js代碼,用以防止圖片撐破頁面

    圖片自動縮小的js代碼,用以防止圖片撐破頁面...
    2007-03-03
  • javascript 讀取xml,寫入xml 實現(xiàn)代碼

    javascript 讀取xml,寫入xml 實現(xiàn)代碼

    javascript xml讀取,寫入xml 實現(xiàn)代碼
    2009-07-07
  • 微信小程序?qū)崿F(xiàn)分享朋友圈的圖片功能示例

    微信小程序?qū)崿F(xiàn)分享朋友圈的圖片功能示例

    這篇文章主要介紹了微信小程序?qū)崿F(xiàn)分享朋友圈的圖片功能,結(jié)合實例形式分析了微信小程序?qū)崿F(xiàn)分享到朋友圈功能的具體操作技巧,需要的朋友可以參考下
    2019-01-01
  • 詳解js中l(wèi)et與var聲明變量的區(qū)別

    詳解js中l(wèi)et與var聲明變量的區(qū)別

    這篇文章主要介紹了let與var聲明變量區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • js console.log打印對象時屬性缺失的解決方法

    js console.log打印對象時屬性缺失的解決方法

    在編寫代碼時,我們常常用 console.log() 的方式將信息在控制臺中打印出來以幫助我們進(jìn)行前端調(diào)試,那么console.log打印對象時屬性缺失怎么辦?下面我們就一起來了解一下解決方法
    2019-05-05
  • 小程序分頁實踐之編寫可復(fù)用分頁組件

    小程序分頁實踐之編寫可復(fù)用分頁組件

    這篇文章主要介紹了小程序分頁實踐之編寫可復(fù)用分頁組件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • 微信小程序?qū)崿F(xiàn)橫向增長表格的方法

    微信小程序?qū)崿F(xiàn)橫向增長表格的方法

    這篇文章主要介紹了微信小程序?qū)崿F(xiàn)橫向增長表格的方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-07-07
  • 全面了解JavaScript的作用域鏈

    全面了解JavaScript的作用域鏈

    這是一個非常重要的知識點了,了解了JavaScript的作用域鏈的話,能幫助我們理解很多‘異?!瘑栴}。文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • 微信小程序?qū)崿F(xiàn)選項卡滑動切換

    微信小程序?qū)崿F(xiàn)選項卡滑動切換

    這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)選項卡滑動切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • JavaScript實現(xiàn)小球沿正弦曲線運動

    JavaScript實現(xiàn)小球沿正弦曲線運動

    這篇文章主要為大家詳細(xì)介紹了JavaScript實現(xiàn)小球沿正弦曲線運動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12

最新評論