честно говоря сразу хотел назвать прототипы с маленькой буквы, потом зачем-то оставил, как в примере по ссылке
|
бывает :)
Я вот теперь не могу тему переименовать. Только название получилось :D |
с чпу наверное уже ничего не сделать
|
Цитата:
|
Ща вам батя покажет класс
|
о нееет, пощади!
|
новая версия Class, к суперметодам обращаемся через $methodName. если свойство начинается или заканчивается на жетсткий побел, то оно становится не итерируемым. вот и вся суть
function Class(){
}
Class.extend = function (Prototype) {
Prototype.prototype = this.prototype;
constructor.prototype = new Prototype();
constructor.extend = this.extend;
function constructor() {
if (this.constructor) {
this.constructor.apply(this, arguments);
}
for (var key in this) if (this.hasOwnProperty(key) && /(^_)|(_$)/.test(key)) {
Object.defineProperty(this, key, {
value : this[key],
writable : true,
configurable: true,
enumerable : false
});
}
}
for (var key in constructor.prototype) if (constructor.prototype.hasOwnProperty(key)) {
if (/(^_)|(_$)|(constructor)/.test(key)) {
Object.defineProperty(constructor.prototype, key, {
value : constructor.prototype[key],
writable : true,
configurable: true,
enumerable : false
});
}
if (key in this.prototype && typeof this.prototype[key] === 'function') {
Object.defineProperty(constructor.prototype, '$' + key, {
value : this.prototype[key],
writable : false,
configurable: false,
enumerable : false
});
}
}
return constructor;
};
//##############################################################
// проверяем суперметоды
alert('проверяем суперметоды')
var Animal = Class.extend(function () {
this.say = function () {
alert('Animal');
}
});
var Cat = Animal.extend(function () {
this.say = function () {
this.$say();
alert('Cat');
}
});
new Cat().say(); // Animal, Cat
// проверяем приватные
alert('проверяем приватные')
var Animal = Class.extend(function () {
this.say = function () {
};
this._need = function () {
}
});
for (var key in new Animal) alert(key) // say
// проверяем instanceof
alert('проверяем instanceof')
alert( new Cat() instanceof Cat ) // true
|
Ну а дальше вы можете расширять этот каркас всякими деструкторами и евент эмиттерами:
var EventEmitter = Class.extend(function () {
this.constructor = function () {
this._handlersStore = {}
};
this.on = function (type, handler) {
var handlersStore = this._handlersStore;
var handlers = handlersStore[type] || (handlersStore[type] = []);
handlers.push(handler);
};
this.emit = function (type) {
var self = this;
var eventArgs = [].slice.call(arguments, 1);
var handlersStore = this._handlersStore;
var handlers = handlersStore[type] || [];
handlers.forEach(function (handler) {
handler.apply(self, eventArgs);
});
};
});
var q = new EventEmitter();
q.on('click', function () {
alert('on click!');
});
q.emit('click');
|
1. Если я правильно понял, то это
function constructor() {
if (this.constructor) {
this.constructor.apply(this, arguments);
}
}
правильней написать так
function constructor() {
if (this.$constructor) this.$constructor.apply(this, arguments);
if (this.constructor) this.constructor.apply(this, arguments);
}
3. Если уж так писать циклы, обертки и т.п. то можно и миксыны сделать 4. Нет той гибкости, кот. есть при использовании ссылки на СуперКласс (на мой взгляд) 5. Кроме того, стороннему разработчику придется разбираться в твоей капусте :) |
Цитата:
|
| Часовой пояс GMT +3, время: 22:48. |