Author: slightboy, Time: 2006-10-17
This article is a JS implementation version. The principle has been explained in the previous work, so I will not explain it here.
If you need an introduction to the principle, please check the VBS version.
var PermissionType =
{
Read : 1,
Write : 2,
Delete : 4
}
function PermissionSetComponent(value)
{
this.Value = value;
this.getRead = function()
{
return this.getValue(PermissionType.Read);
}
this.setRead = function(value)
{
this. setValue(PermissionType.Read, value);
}
this.Read = function()
{
if (arguments.length > 0)
this.setValue(PermissionType.Read, arguments [0]).; )
this.setValue(PermissionType.Write, arguments[0]);
else
return this.getValue(PermissionType.Write);
}
this.Delete = function( )
{
if (arguments.length > 0)
this.setValue(PermissionType.Delete, arguments[0]);
else
return this.getValue(Per missionType.Delete);
}
this.getValue = function(permissionType)
{
return (this.Value & permissionType) == permissionType;
}
this.setValue = function(permissionType, value)
{
if (value)
this.Value |= permissionType;
else
this.Value &= ~permissionType;
}
}
var PermissionSet = new PermissionSetComponent(0);
w("Read:");
PermissionSet.Read(false);
w(PermissionSet.Value " " PermissionSet.Read());
PermissionSet.Read( true);
w(PermissionSet.Value " " PermissionSet.Read());
w("Write:");
PermissionSet.Write(false);
w(PermissionSet.Value " " PermissionSet.Write());
PermissionSet.Write(true);
w(PermissionSet.Value " " " PermissionSet.Write());
w("Delete:");
PermissionSet .Delete(false);
w(PermissionSet.Value " " PermissionSet.Delete());
PermissionSet.Delete(true);
w(PermissionSet.Value " " PermissionSet.Delete());
function w(o)
{
Response.Write(o "
");
}
Note: The red part is Java style writing and is not required for this example.
Just for display, if you prefer java style, you can also choose this writing method.