基于邏輯運算的簡單權(quán)限系統(tǒng)(實現(xiàn)) JS 版
更新時間:2007年03月24日 00:00:00 作者:
作者: slightboy, 時間: 2006-10-17
此篇為 JS 實現(xiàn)版本, 以前作已交待原理 故不在此多做解釋
如需原理介紹 請查看 VBS 版.
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 風格也可以選擇這種寫法.
此篇為 JS 實現(xiàn)版本, 以前作已交待原理 故不在此多做解釋
如需原理介紹 請查看 VBS 版.
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 風格也可以選擇這種寫法.
相關(guān)文章
JavaScript實現(xiàn)簡易購物車最全代碼解析(ES6面向?qū)ο?
這篇文章主要為大家詳細介紹了JavaScript實現(xiàn)簡易購物車最全代碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09JavaScript對象字面量和構(gòu)造函數(shù)原理與用法詳解
這篇文章主要介紹了JavaScript對象字面量和構(gòu)造函數(shù),結(jié)合實例形式分析了JavaScript對象字面量和構(gòu)造函數(shù)相關(guān)概念、原理、用法及操作注意事項,需要的朋友可以參考下2020-04-04JS獲取CSS樣式(style/getComputedStyle/currentStyle)
這篇文章主要為大家介紹了JS獲取CSS樣式的方法,介紹了CSS樣式的三種分類情況,對獲取樣式做一個簡單的封裝,感興趣的小伙伴們可以參考一下2016-01-01最全正則表達式總結(jié):驗證QQ號、手機號、Email、中文、郵編、身份證、IP地址等
這篇文章主要介紹了最全正則表達式:驗證QQ號、手機號、Email、中文、郵編、身份證、IP地址等,通過語法介紹作用表達式等詳細解釋了正則表達式的使用,具體操作步驟大家可查看下文的詳細講解,感興趣的小伙伴們可以參考一下。2017-08-08