-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample2.js
58 lines (45 loc) · 1.12 KB
/
example2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
var huskies = require("huskies"),
strict = require("./");
// set converter for Type.
strict.converter(Type,function converter(value){
console.log(value)
if(value.name){
return new Type(value.name);
}else{
throw new Error("convert Type error!");
}
})
// set validater for Type.
strict.validater(Type,function validater(value,params){
if(params.len && value.name.length === params.len){
}else{
throw new Error;
}
});
function Type(name){
this.name = name;
}
function Article(){
this._name = null;
this._type = null;
}
Article.prototype = {
setName:
huskies(function(name){
this._name = name;
})
.use(strict)
.set({type:String}),
setType:
huskies(function(type){
this._type = type;
})
.use(strict)
.set({type:Type,params:{len:5}})
}
var article = new Article();
article.setType({name:"ttttt"});
console.log(article._type instanceof Type) // true
console.log(article._type.name) // ttttt
article.setType(new Type("aaaaa"));
console.log(article._type.name) // ttttt