|
|
function buildClass(){
|
|
|
/*变量*/
|
|
|
var supr = this,
|
|
|
parms=[].slice.call(arguments)||[],
|
|
|
toString={}.toString,
|
|
|
slice=[].slice,
|
|
|
__step="__";
|
|
|
|
|
|
/*方法*/
|
|
|
function __getType(value){
|
|
|
if(["boolean","string","number","undefined"].indexOf((typeof value).toLowerCase())>-1){
|
|
|
return (typeof value).toLowerCase();
|
|
|
}
|
|
|
return toString.call(value).replace(/\[object\s*([^\]]+)\s*\]/,'$1').toLowerCase();
|
|
|
};
|
|
|
function __isFun(value){
|
|
|
return typeof value=="function";
|
|
|
};
|
|
|
function __isStr(value){
|
|
|
return typeof value=="string";
|
|
|
};
|
|
|
function __isBoolen(value){
|
|
|
return typeof value=="boolean";
|
|
|
}
|
|
|
function __upName(name){
|
|
|
return name.charAt(0).toUpperCase()+name.substring(1);
|
|
|
};
|
|
|
|
|
|
function __lowerName(name){
|
|
|
return "__"+name+"__";
|
|
|
};
|
|
|
function __build(items,callback){
|
|
|
if(toString.call(items)!=="[object Array]"){
|
|
|
items=[items];
|
|
|
}
|
|
|
while(item=items.shift()){
|
|
|
for(var key in item){
|
|
|
if(item.hasOwnProperty(key)){
|
|
|
callback&&callback(item[key],key);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
function __error(value){
|
|
|
return {code:200,message:value};
|
|
|
};
|
|
|
function __guid(){
|
|
|
return Date.now()+Math.random().toString(36).substring(2, 15);
|
|
|
};
|
|
|
function __validate(isval,value,name){
|
|
|
if(__isBoolen(isval)&&isval&&this.__validations__[name]){
|
|
|
if(__getType(value)!=this.__feild__[name].type){
|
|
|
throw __error(name+" type error");
|
|
|
}
|
|
|
if(!this.__validations__[name].call(this)){
|
|
|
throw __error(name+" validate error");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
var __rule={
|
|
|
require:function(value,rules){
|
|
|
return Boolean(value.replace(/^\s+|\s+$/,''));
|
|
|
},
|
|
|
ranges:function(value,rules){
|
|
|
return new RegExp("^["+rules+"]*$").test(value);
|
|
|
},
|
|
|
length:function(value,rules){
|
|
|
var r=rules.split(',');
|
|
|
if(r[0]&&value.length<r[0]){
|
|
|
return false;
|
|
|
}
|
|
|
if(r[1]&&value.length>r[1]){
|
|
|
return false;
|
|
|
}
|
|
|
return true;
|
|
|
},
|
|
|
type:function(value,type){
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
function customClass(){};
|
|
|
customClass.prototype=this.prototype;
|
|
|
customClass.prototype.__validations__={};
|
|
|
var prototype=new customClass();
|
|
|
|
|
|
|
|
|
function subClass(obj){
|
|
|
var that=this;
|
|
|
|
|
|
for(var name in that.__feild__){
|
|
|
if(that.__feild__.hasOwnProperty(name)){
|
|
|
that[name]=(obj&&obj[name])||that.__feild__[name].value;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
subClass.__create=function(parms){
|
|
|
var isValObj=false,
|
|
|
me=this;
|
|
|
while(parm=parms.shift()){
|
|
|
if(!isValObj){
|
|
|
//验证规则
|
|
|
__build(parm,function(obj,name){
|
|
|
|
|
|
var verify=prototype.__validations__;
|
|
|
|
|
|
if(!prototype.__feild__.hasOwnProperty(name)){
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
if(__isFun(obj)){
|
|
|
verify[name]=(function(obj){
|
|
|
return function(){
|
|
|
return Boolean(obj.apply(this,arguments));
|
|
|
}
|
|
|
})(obj);
|
|
|
}else if(__isStr(obj)){
|
|
|
if(/^@\{[^}]+\}[img]{0,1}/.test(obj)){
|
|
|
var rules=obj.match(/^@\{([^}]+)\}([img]{0,1})/);
|
|
|
var regex=new RegExp(rules[1],rules[2]);
|
|
|
verify[name]=function(){
|
|
|
return regex.test(this[name]);
|
|
|
}
|
|
|
}else{
|
|
|
validateObj[name]=function(){
|
|
|
var items=obj.split('.'),item;
|
|
|
while(item=items.shift()){
|
|
|
var parms=item.match(/([^(]+)(\(([^)]+)\))?/);
|
|
|
if(!__rule[parms[1]](this[name].value,prototype[__lowerName(name)].type,parms[3])){
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
}else{
|
|
|
throw "error";
|
|
|
}
|
|
|
});
|
|
|
isValObj=true;
|
|
|
continue;
|
|
|
}
|
|
|
__build(parm,function(obj,key){
|
|
|
if(__isFun(obj)){
|
|
|
prototype[key]=(function(parm,key){
|
|
|
return function(){
|
|
|
return parm[key].apply(this,arguments);
|
|
|
}
|
|
|
})(parm,key)
|
|
|
}else{
|
|
|
prototype[key]=parm[key];
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
this.prototype=prototype;
|
|
|
return this;
|
|
|
};
|
|
|
subClass.__create.call(subClass,parms).prototype.constructor=subClass;
|
|
|
|
|
|
//对象构造器
|
|
|
subClass.define=function(obj,fn){
|
|
|
|
|
|
var that=subClass.prototype;
|
|
|
that.__feild__={};
|
|
|
|
|
|
obj=typeof obj==="string"?(function(){
|
|
|
var o={};
|
|
|
o[obj]=fn;
|
|
|
return o;
|
|
|
}):obj;
|
|
|
|
|
|
__build([obj["__proto__"],obj],function(obj,name){
|
|
|
var prototype=that,guid=__guid();
|
|
|
prototype.__feild__[name]={
|
|
|
type:__getType(obj),
|
|
|
value:obj,
|
|
|
origin:name
|
|
|
};
|
|
|
prototype["get"+__upName(name)]=(function(name){
|
|
|
return function(){
|
|
|
// var args=slice.call(arguments,0);
|
|
|
// __validate.call(this,args[0],name);
|
|
|
return this[name];
|
|
|
}
|
|
|
})(name);
|
|
|
prototype["set"+__upName(name)]=(function(name){
|
|
|
return function(value){
|
|
|
var args=slice.call(arguments,0);
|
|
|
__validate.call(this,args[1],args[0],name);
|
|
|
this[name]=args[0];
|
|
|
}
|
|
|
})(name);
|
|
|
});
|
|
|
return this;
|
|
|
};
|
|
|
|
|
|
/*构建Class*/
|
|
|
subClass.extend=arguments.callee;
|
|
|
return subClass;
|
|
|
}
|
|
|
exports.module=function(obj){
|
|
|
return buildClass.call(obj||function(){});
|
|
|
}
|
|
|
|
|
|
|
|
|
/*
|
|
|
var LClass=new BuildClass();
|
|
|
|
|
|
var UserClass=LClass.define({
|
|
|
firstName:String("wei"),
|
|
|
lastName:String("qingting"),
|
|
|
list:Array()
|
|
|
}).extend({
|
|
|
// 1. 规则 2.正则 3.方法
|
|
|
firstName:"require.ranges(1-9).length(1,10)",
|
|
|
lastName:"@{^\\w+$}",
|
|
|
list:function(){
|
|
|
if(this.getList().length==0){
|
|
|
return false;
|
|
|
}else{
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
},{
|
|
|
sex:"男",
|
|
|
fullName:function(){
|
|
|
return this.getFirstName()+this.getLastName();
|
|
|
}
|
|
|
});
|
|
|
|
|
|
var u=new UserClass({lastName:"lan"});
|
|
|
try{
|
|
|
u.setLastName(1,true)
|
|
|
console.log(u.getLastName());
|
|
|
}
|
|
|
catch(ex){
|
|
|
console.log(ex);
|
|
|
}*/ |
|
|
\ No newline at end of file |
...
|
...
|
|