How do I access the outer property from inside?
Hi I am trying to emulate a bookcase. My problem is how to access this.MAX_WIDTH from inside object method Add()... 开发者_开发技巧??? :-( how?
function bookCase(){
this.bcname="new Book Case 1";
this.showName=function(){return this.name;}
this.MAX_WIDTH=50; // 0 for unlimited :-)
this.MAX_HEIGHT=0; // Unlimited by default :-)
this.MAX_SHELVES=0; // Unlimited No. of Shelves by Default :-)
this.currrWidth = 0; // Nothing Kept Yet :-)
this.currrHeight= 0; // Nothing Kept Yet :-)
this.currWidth=0;
this.showCapacity = function(MW,MH,MS){
var ReturnVal="";
if(MW){ if(ReturnVal!="")
{
ReturnVal =","+ ReturnVal + this.MAX_WIDTH;
} else {
ReturnVal = ReturnVal + this.MAX_WIDTH;
}
}
if(MH){ if(ReturnVal!="")
{
ReturnVal = ReturnVal + "," + this.MAX_HEIGHT;
} else {ReturnVal = ReturnVal + this.MAX_HEIGHT;}
}
if(MS){ if(ReturnVal!="")
{
ReturnVal = ReturnVal + "," + this.MAX_SHELVES;
} else {ReturnVal = ReturnVal + this.MAX_SHELVES;}
}
if(!MW&&!MH&&!MS){return this.MAX_WIDTH + "," + this.MAX_HEIGHT + "," + this.MAX_SHELVES;}
return ReturnVal;
}
this.shelf =
{
books : new Array(),
book :
{
add : function(Place,Width)
{
if(window.bookCase.MAX_WIDTH > Width)
{
this.books[Place]=Width; alert("book Added");
}
else {alert("No Space In BookShelf: Max Width is "+window.bookCase.MAX_WIDTH); alert(window.bookCase.bcname);}
}
}
}
}
var bookStack = new Array();
bookStack[0] = 10;
bookStack[1] = 20;
/* bookStack[2] = 20;
bookStack[3] = 10;
bookStack[4] = 10;
bookStack[5] = 10;
bookStack[6] = 10;
bookStack[7] = 10;
bookStack[8] = 10;
bookStack[9] = 10;
bookStack[10] = 10;
*/
var foo = new bookCase;
var LC =0;
for(LC=0;LC<=bookStack.length;LC++)
{
foo.shelf.book.add(LC,bookStack[LC]);
}
try defining your properties as internal variables, then you can create a property that returns that variable if you like
function bookCase(){
var Max_Width =50;
this.MAX_WIDTH=Max_width; // 0 for unlimited :-)
and in add
if(Max_Width > Width)
精彩评论