-
Notifications
You must be signed in to change notification settings - Fork 16
/
behavior.js
39 lines (38 loc) · 1.06 KB
/
behavior.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
"use strict";
var behaviorsMap = {};
function behavior(name, behavior){
if(typeof name !== "string") {
behavior = name;
name = undefined;
}
var behaviorMixin = function(base){
// basically Object.create
var Behavior = function(){};
Object.defineProperty(Behavior,"name",{
value: name,
configurable: true
});
Behavior.prototype = base;
var newBehavior = new Behavior();
// allows behaviors to be a simple object, not always a function
var res = typeof behavior === "function" ? behavior.apply(newBehavior, arguments) : behavior;
for(var prop in res) {
if(res.hasOwnProperty(prop)) {
Object.defineProperty(newBehavior, prop, Object.getOwnPropertyDescriptor(res, prop));
} else {
// we only copy values from up the proto chain
newBehavior[prop] = res[prop];
}
}
newBehavior.__behaviorName = name;
return newBehavior;
};
if(name) {
behaviorMixin.behaviorName = name;
behaviorsMap[name] = behaviorMixin;
}
behaviorMixin.isBehavior = true;
return behaviorMixin;
}
behavior.map = behaviorsMap;
module.exports = behavior;