首頁  >  文章  >  web前端  >  js初始化驗證實例詳解

js初始化驗證實例詳解

高洛峰
高洛峰原創
2016-12-05 10:20:25999瀏覽

本文實例講述了js初始化驗證的方法。分享給大家參考,具體如下:

<script type="text/javascript">
var Book = function(isbn, title, author) {
 if(!this.checkIsbn(isbn)){
   throw new Error(&#39;Book: Invalid ISBN.&#39;);
 } 
 this.isbn = isbn;
 this.title = title || &#39;No title specified&#39;;
 this.author = author || &#39;No author specified&#39;;
}
Book.prototype = {
 checkIsbn: function(isbn) {
  if(isbn == undefined || typeof isbn != &#39;string&#39;) {
   return false;
  }
  return true; // All tests passed.
 },
 display: function() {
  alert("isbn:"+this.isbn+" title:"+this.title+" author:"+this.author);
 }
};
var theHobbit = new Book(&#39;0-395-07122-4&#39;, &#39;The Hobbit&#39;, &#39;J. R. R. Tolkein&#39;);
theHobbit.display(); // Outputs the data by creating and populating an HTML element.
</script>

對isbn進行驗證。是否定義,是否為字串等等。對title進行判斷,設定預設。

另一種實現方式

<script type="text/javascript">
/* 出版 interface. */
/* var Publication = new Interface(&#39;Publication&#39;, [&#39;getIsbn&#39;, &#39;setIsbn&#39;, &#39;getTitle&#39;,
 &#39;setTitle&#39;, &#39;getAuthor&#39;, &#39;setAuthor&#39;, &#39;display&#39;]); */
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 != &#39;string&#39;) {
   return false;
  }
  return true; // All tests passed.
 },
 getIsbn: function() {
  return this.isbn;
 },
 setIsbn: function(isbn) {
  if(!this.checkIsbn(isbn)) throw new Error(&#39;Book: Invalid ISBN.&#39;);
  this.isbn = isbn;
 },
 getTitle: function() {
  return this.title;
 },
 setTitle: function(title) {
  this.title = title || &#39;No title specified&#39;;
 },
 getAuthor: function() {
  return this.author;
 },
 setAuthor: function(author) {
  this.author = author || &#39;No author specified&#39;;
 },
 display: function() {
  alert("isbn:"+this.isbn+" title:"+this.title+" author:"+this.author);
 }
};
var theHobbit = new Book(&#39;0-395-07122-4&#39;, &#39;&#39;, &#39;J. R. R. Tolkein&#39;);
theHobbit.display(); // Outputs the data by creating and populating an HTML element.
</script>

接口實現,參考接口,定義了很多方法。

內部方法命名加_,例如這個檢測的方法 _checkIsbn

<script type="text/javascript">
/* 出版 interface. */
/* var Publication = new Interface(&#39;Publication&#39;, [&#39;getIsbn&#39;, &#39;setIsbn&#39;, &#39;getTitle&#39;,
 &#39;setTitle&#39;, &#39;getAuthor&#39;, &#39;setAuthor&#39;, &#39;display&#39;]); */
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 != &#39;string&#39;) {
   return false;
  }
  return true; // All tests passed.
 },
 getIsbn: function() {
  return this.isbn;
 },
 setIsbn: function(isbn) {
  if(!this._checkIsbn(isbn)) throw new Error(&#39;Book: Invalid ISBN.&#39;);
  this.isbn = isbn;
 },
 getTitle: function() {
  return this.title;
 },
 setTitle: function(title) {
  this.title = title || &#39;No title specified&#39;;
 },
 getAuthor: function() {
  return this.author;
 },
 setAuthor: function(author) {
  this.author = author || &#39;No author specified&#39;;
 },
 display: function() {
  alert("isbn:"+this.isbn+" title:"+this.title+" author:"+this.author);
 }
};
//var theHobbit = new Book(123, &#39;&#39;, &#39;J. R. R. Tolkein&#39;); // 非字符串抛出异常
var theHobbit = new Book(&#39;1990-78sd-1092&#39;, &#39;&#39;, &#39;J. R. R. Tolkein&#39;); 
theHobbit.display(); // Outputs the data by creating and populating an HTML element.
</script>


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn