log = console.log.bind(console);
(function() {
// пространства свойств
var propSpases = ["__private__", "__privateStatic__", "__public__",
"__publicStatic__"];
// установить свойство
function setProp(obj, propSpase, name, value) {
obj[propSpase][name] = (typeof value === 'function') ? value
.bind(getSelf(obj)) : value;
return true
};
// собрать self объект
function getSelf(obj) {
var self = Proxy
.create({
get : function(proxy, name) {
switch (name) {
case 'public' :
alert('public')
break;
case 'private' :
alert('private')
break;
case 'static' :
alert('static')
break;
default :
for ( var i = 0; i < propSpases.length; i++) {
if (obj[propSpases[i]].hasOwnProperty(name)) {
return obj[propSpases[i]][name]
}
}
}
},
set : function(proxy, name, value) {
switch (name) {
case 'public' :
alert('public')
break;
case 'private' :
alert('private')
break;
case 'static' :
alert('static')
break;
default :
for ( var i = 0; i < propSpases.length; i++) {
if (obj[propSpases[i]].hasOwnProperty(name)) {
return setProp(obj, propSpases[i],
name, value);
}
}
return false
}
}
})
return self
};
window['Class'] = function(classDescription) {
// создаем конструктор
var constructor = function() {
// создаем персональные наборы свойств сущности
var prop = {};
prop.__private__ = {};
prop.__privateStatic__ = constructor.private;
prop.__public__ = {};
prop.__publicStatic__ = constructor.public;
// создаем инициализатор
var init = {
private : function(properts) {
for ( var i in properts) {
setProp(prop, '__private__', i, properts[i]);
}
return true
},
public : function(properts) {
for ( var i in properts) {
setProp(prop, '__public__', i, properts[i]);
}
return true
},
privateStatic : function(properts) {
for ( var i in properts) {
setProp(prop, '__privateStatic__', i, properts[i]);
}
return true
},
publicStatic : function(properts) {
for ( var i in properts) {
setProp(prop, '__publicStatic__', i, properts[i]);
}
return true
}
};
// инициализируем свойства
classDescription.apply(init, arguments)
// возвращаем интерфейс для работы с публичными свойствами
return Proxy.create({
get : function(proxy, name) {
if (prop.__publicStatic__.hasOwnProperty(name)) {
return prop.__publicStatic__[name]
}
if (prop.__public__.hasOwnProperty(name)) {
return prop.__public__[name]
}
},
set : function(proxy, name, value) {
if (prop.__public__.hasOwnProperty(name)) {
prop.__public__[name] = value;
return true;
}
if (prop.__publicStatic__.hasOwnProperty(name)) {
prop.__publicStatic__[name] = value;
return true;
}
prop.__public__[name] = value;
return true;
},
getOwnPropertyDescriptor : function(name) {
if (prop.__public__.hasOwnProperty(name)) {
return Object.getOwnPropertyDescriptor(prop.__public__,
name)
}
if (prop.__publicStatic__.hasOwnProperty(name)) {
return Object.getOwnPropertyDescriptor(
prop.__publicStatic__, name)
}
},
getOwnPropertyNames : function() {
var names = Object.getOwnPropertyNames(prop.__public__)
return names.concat(Object
.getOwnPropertyNames(prop.__publicStatic__))
},
getPropertyDescriptor : function() {
alert('getPropertyDescriptor')
},
getPropertyNames : function() {
alert('getPropertyNames')
},
defineProperty : function() {
alert('defineProperty')
},
'delete' : function() {
alert('delete')
},
hasOwn : function() {
alert('hasOwn')
},
enumerate : function() {
alert('enumerate')
},
keys : function() {
alert('keys')
}
}, prop)
}
// создаем статические наборы свойств сущностей
constructor.private = {}
constructor.public = {}
// возвращаем конструктор
return constructor
}
})()
var Cat = Class(function() {
this.private({
'name' : 'приватное имя',
'age' : 200,
'go' : function() {
alert('go')
}
});
this.public({
'w' : function() {
alert('количество созданных: ' + this.col)
},
'pub' : 0
});
this.privateStatic({
'a' : 0
})
this.publicStatic({
'ps' : 'publicStatic'
})
})
var q = Cat()
var w = Cat()
log(q)
log(w)