Heim  >  Artikel  >  Web-Frontend  >  Detaillierte Erläuterung des Beispiels zur Überprüfung der JS-Initialisierung

Detaillierte Erläuterung des Beispiels zur Überprüfung der JS-Initialisierung

高洛峰
高洛峰Original
2016-12-05 10:20:25964Durchsuche

Das Beispiel in diesem Artikel beschreibt die Methode zur Überprüfung der js-Initialisierung. Teilen Sie es allen als Referenz mit. Die Details lauten wie folgt:

<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 überprüfen. Ob es definiert ist, ob es sich um eine Zeichenfolge handelt usw. Beurteilen Sie den Titel und legen Sie die Standardeinstellung fest.

Eine andere Implementierungsmethode

<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>

Schnittstellenimplementierung, Referenzschnittstelle, definiert viele Methoden.

Interne Methodennamen werden mit _ hinzugefügt, zum Beispiel diese Erkennungsmethode _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>


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn