Simple permission system based on logic operation (realization) JS version _javascript skill

Source: Internet
Author: User
Author: slightboy, Time: 2006-10-17
This is a JS implementation version, previously made to explain the principle is not here to do more explanation
For a description of the principle, please view 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]);
Else
Return This.getvalue (Permissiontype.read);
}
This. Write = function ()
{
if (Arguments.length > 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 (Permissiontype.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 + "<br/>");
}
Note: The red section for Java style writing is not necessary for this example.
Just do a show, if you prefer Java style, you can choose this type of writing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.