Home > Web Front-end > JS Tutorial > Simple permission system based on logical operations (implementation) JS version_javascript skills

Simple permission system based on logical operations (implementation) JS version_javascript skills

WBOY
Release: 2016-05-16 19:16:22
Original
1072 people have browsed it

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template