PDA

View Full Version : Using Array as Stack



sekaijin
24 Jul 2008, 12:30 AM
Hi
The Javascript array object support
Properties :

length

Methods :

concat()
join()
pop()
push()
reverse()
shift()
slice()
splice()
sort()
unshift()

Ext add 2 methods

indexOf()
remove()


if your are using an array as stack (LIFO) you can use push and pop to add an remove item in stack
But you can't get the top of the stack. you need to pop and push or calculate the legnth of the stack to get the last item.
this sample code add top method tio get last item.

/**
* @class Array
*/
Ext.applyIf(Array.prototype, {
/**
* Return the last object in array
* @return {Object}|null The objec in the array (or null if it is not found)
*/
top : function(){
if (0 == this.length){
return null;
}
return this[this.length -1];
}
});

now you have all methods you need to use array as stack

pop()
push()
top()


A+JY
PS : sorry for my approximative english