这是一个提供简单字典的 JavaScript 类。
if( typeof( rp ) == "undefined" ) rp = {};
rp.clientState = new function()
{
this.items = new Object();
this.length = 0;
this.set = function( key, value )
{
if ( ! this.keyExists( key ) )
{
this.length++;
}
this.items[ key ] = value;
}
this.get = function( key )
{
if ( this.keyExists( key ) )
{
return this.items[ key ];
}
}
this.keyExists = function( key )
{
return typeof( this.items[ key ] ) != "undefined";
}
this.remove = function( key )
{
if ( this.keyExists( key ) )
{
delete this.items[ key ];
this.length--;
return true;
}
return false;
}
this.removeAll = function()
{
this.items = null;
this.items = new Object();
this.length = 0;
}
}
示例使用:
// Add a value pair.
rp.clientState.set( key, value );
// Fetch a value.
var x = rp.clientState.Get( key );
// Check to see if a key exists.
if ( rp.clientState.keyExists( key )
{
// Do something.
}
// Remove a key.
rp.clientState.remove( key );
// Remove all keys.
rp.clientState.removeAll();