javascript設計モード4
6006 ワード
静的なメンバーは直接クラスのオブジェクトを通して訪問します.
var Book=(function(){
var numOfBooks=0;
function checkIsbn(isbn){
...
}
return function(newIsbn,newTitle,newAuthor){
var isbn,title,author;
this.getIsbn=function(){
return isbn;
};
this.setIsbn=function(newIsbn){
if(!checkIsbn(newIsbn)) throw new Error('ISBN ');
isbn=newIsbn;
};
this.getTitle=function(){
return title;
};
this.setTitle=function(newTitle){
title=newTitle||' ';
};
this.getAuthor=function(){
return author;
};
this.setAuthor=function(newAuthor){
author=newAuthor||' ';
};
numOfBooks++;
if(numOfBooks>50) throw new Error(' 50 ');
this.setIsbn(newIsbn);
this.setTitle(newTitle);
this.setAuthor(newAuthor);
}
})();
Book.convertToTitleCase=function(inputString){
...
};
Book.prototype={
display:function(){
...
}
};
静的特権方法(通常の物まね)//Class.getUPPER_BOUND();
var Class=(function(){
var UPPER_BOUND=100;
var ctor=function(constructorArgument){
...
};
ctor.getUPPER_BOUND=function(){
return UPPER_BOUND;
};
...
return ctor;
})();
一般的な採値器の方法//Class.getConstant('UPPER_BOUND');
var Class=(function(){
var constants={
UPPER_BOUND:100,
LOWER_BOUND:-100
};
var ctor=function(constructorArgument){
...
};
ctor.getConstant=function(name){
return constants[name];
};
...
return ctor;
})();
原型チェーンfunction Author(name,books){
Person.call(this,name);
this.books=books;
}
Author.prototype=new Person();
Author.prototype.constructor=Author;
Author.prototype.getBooks = function() {
return this.books;
};