惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
小众软件
小众软件
The Cloudflare Blog
S
SegmentFault 最新的问题
美团技术团队
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
阮一峰的网络日志
阮一峰的网络日志
博客园 - 聂微东
V
Visual Studio Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
雷峰网
雷峰网
WordPress大学
WordPress大学
T
Tailwind CSS Blog
腾讯CDC
人人都是产品经理
人人都是产品经理
月光博客
月光博客
Microsoft Azure Blog
Microsoft Azure Blog
M
MIT News - Artificial intelligence
D
DataBreaches.Net
Apple Machine Learning Research
Apple Machine Learning Research
P
Proofpoint News Feed
J
Java Code Geeks
宝玉的分享
宝玉的分享

博客园 - jayu

Windows 资源管理中 创建无文件名只有扩展名文件 CoreOS Hyper-V 安装 RancherOS Hyper-V 安装 代码片段 MSSQL 2005 分页分析及优化 泛型类型的返回 招 .Net 网站程序员, Flash 程序员 上海盛大网络浩方在线招聘网站程序 ACS 20070412 TODO JSmarty ACS 20070108 更新 Alienwave.CommunityServer 20070103 更新 基于逻辑运算的简单权限系统(原理,设计,实现) VBS 版 ACS 社区系统演示地址 Python 2.5 发布 Visual Studio 2005 SDK version 3.0 道德沦丧 还是意识淡薄 Microsoft Expression Web Beta 1 《星际之剑》(Sword of the Stars)CLONE版
基于逻辑运算的简单权限系统(实现) JS 版
jayu · 2006-10-17 · via 博客园 - jayu

作者: slightboy, 时间: 2006-10-17

此篇为 JS 实现版本, 以前作已交待原理 故不在此多做解释

如需原理介绍 请查看 VBS 版.

 首发于: http://cs.alienwave.cn/Topic/356.aspx

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 />");
}

注: 红色部分为 java 风格写法 不是本例所必须.

只是做一个展示, 如果你比较喜欢 java 风格也可以选择这种写法.