>  기사  >  웹 프론트엔드  >  js 초기화 검증 예시에 대한 자세한 설명

js 초기화 검증 예시에 대한 자세한 설명

高洛峰
高洛峰원래의
2016-12-05 10:20:25969검색

이 기사의 예에서는 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을 확인하세요. 정의되어 있는지, 문자열인지 등 제목을 판단하고 기본값을 설정하세요.

또 다른 구현 방법

<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으로 문의하세요.