国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

js初始化驗證實例詳解

2019-11-19 18:48:47
字體:
來源:轉載
供稿:網友

本文實例講述了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>

更多關于JavaScript相關內容可查看本站專題:《javascript面向對象入門教程》、《JavaScript中json操作技巧總結》、《JavaScript切換特效與技巧總結》、《JavaScript查找算法技巧總結》、《JavaScript動畫特效與技巧匯總》、《JavaScript錯誤與調試技巧總結》、《JavaScript數據結構與算法技巧總結》、《JavaScript遍歷算法與技巧總結》及《JavaScript數學運算用法總結

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鄂托克旗| 镇安县| 呈贡县| 涪陵区| 临武县| 高雄市| 修文县| 浦县| 黔江区| 怀柔区| 肇东市| 石渠县| 施甸县| 临江市| 竹山县| 溆浦县| 金寨县| 康平县| 宁蒗| 玉门市| 崇仁县| 疏附县| 东源县| 松原市| 翁牛特旗| 施甸县| 台江县| 德保县| 体育| 宜兰市| 内丘县| 屏东市| 凌云县| 文化| 察隅县| 石狮市| 嘉禾县| 大埔县| 白山市| 赣州市| 达孜县|