forked from ipcortex/Masquerade-JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmasquerade.js
84 lines (79 loc) · 2.74 KB
/
masquerade.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* Masquerade JS
*
* IP Cortex Ltd
*
* Author: Matthew Preskett
* Co-Author: Steve Davies
*
* https://github.com/ipcortex/Masquerade-JS
*/
(function() {
Class = function(definition) {
var Construct = function Class() {
var object = Object.create(Inner);
for ( var property in definition ) {
if ( property === 'construct' )
continue;
if ( property.substr(0, 1) === '_' || property.substr(0, 1) === '$' )
continue;
if ( typeof(definition[property]) === 'object' ) {
if ( property === 'defineProperties' ) {
var properties = {};
for ( var define in definition[property] ) {
properties[define] = properties[define] || {};
for ( var operator in definition[property][define] )
properties[define][operator] = object['_property_' + define + '_' + operator].bind(object);
}
Object.defineProperties(this, properties);
} else
this[property] = definition[property];
} else if ( typeof(object[property]) === 'function' )
this[property] = object[property].bind(object);
}
if ( typeof(definition.construct) === 'function' ) {
object.$public = this;
definition.construct.apply(object, arguments);
delete object.$public;
}
};
Construct.extend = function(extension) {
for ( var property in definition ) {
if ( property === 'defineProperties' )
continue;
if ( definition.hasOwnProperty(property) && ! extension.hasOwnProperty(property))
extension[property] = definition[property];
}
return new Class(extension);
};
/* Construct Inner from Construct so 'instanceof' works */
var Inner = Object.create(Construct.prototype);
for ( var property in definition ) {
if ( ! definition.hasOwnProperty(property) )
continue;
if ( property.substr(0, 1) === '$' )
Construct[property.substr(1)] = definition[property];
else if ( property === 'defineProperties' ) {
for ( var define in definition[property] ) {
for ( var operator in definition[property][define] )
Inner['_property_' + define + '_' + operator] = definition[property][define][operator];
}
} else if ( typeof(definition[property]) !== 'function' ) {
var sharedStorage = {value: definition[property]};
var getNset = {
get: function() { return this.value; }.bind(sharedStorage),
set: function(v) { this.value = v; }.bind(sharedStorage)
};
if ( property.substr(0, 1) !== '_' ) {
Object.defineProperty(Construct.prototype, property, getNset);
Object.defineProperty(Construct, property, getNset);
} else
Object.defineProperty(Inner, property, getNset);
} else /* type == 'function' */
Inner[property] = definition[property];
}
Construct._isClass = true;
return Construct;
};
Class._isClass = true;
})();