Я использовал специально созданную библиотеку для обработки объектов (для экзаменов не могу использовать вещи, которые я не программирую на личном уровне, например JS.class), которая имеет тенденцию нарушать область действия функций.
Файл base.js
/**
* Module internals
*/
function _add() {
var obj_out = arguments[0];
for (var i=1; i < arguments.length; i++) { var arg = arguments[i];
for(prop in arg) { obj_out[prop] = arg[prop]; }
};
return obj_out;
}
function _merge() {
var obj_out = {};
for (var i=1; i < arguments.length; i++) { var arg = arguments[i];
for(prop in arg) { obj_out[prop] = arg[prop]; }
};
return obj_out;
}
function _args(args) {
return Array.prototype.slice.call(args, 0);
}
function _filterProps(items, re) {
console.log("Items: ", items);
var obj_out = {};
console.log("Before: ", obj_out);
var keys = [];
for(var key in items) {
keys.push(key);
}
console.log("Keys: ", keys);
for (var i=0; i < keys.length; i++) { var key = keys[i];
if(!re.test(key)){
obj_out[key] = items[key];
console.log("Step: ", obj_out);
}
}
console.log("After: ", obj_out);
return obj_out;
}
/**
* Class declaration
* Usage:
{
$extends: <classe parente>
$include: [<modules>]
$new: <constructeur>
$static: <methodes statiques>
$methods: <methodes>
}
*/
exports.Class = function(items) {
var base = !items["$extends"] ? Object.prototype : items["$extends"].prototype;
var incs = !items["$include"]? [] : items["$include"];
var stat = !items["$static"] ? {} : items["$static"];
var meth = !items["$methods"] ? {} : items["$methods"];
var cons = !items["$new"] ? function(){} : items["$new"];
var left = _filterProps(items, /^\$/);
var cls = function() {
console.log("Constructor");
console.log(arguments);
cons.apply(this, _args(arguments));
};
cls = _add(cls, stat);
cls.prototype = base;
for (var i=0; i < incs.length; i++) {
_add(cls.prototype, incs[i]);
};
_add(cls.prototype, left);
_add(cls.prototype, meth);
return cls;
}
Файл test.js
Base = require('./base');
var SomeClass = Base.Class({
$new: function() { console.log("new"); },
foo: function() { console.log("foo"); },
});
var OtherClass = Base.Class({
$new: function() { console.log("new"); },
bar: function() { console.log("bar"); }
});
console.log("SomeClass: ", SomeClass.prototype);
console.log("OtherClass: ", OtherClass.prototype);
Вывод "node test.js"
Items: { '$new': [Function], foo: [Function] }
Before: {}
Keys: [ '$new', 'foo' ]
Step: { foo: [Function] }
After: { foo: [Function] }
Items: { '$new': [Function], bar: [Function] }
Before: {}
Keys: [ '$new', 'bar', 'foo' ]
Step: { bar: [Function] }
Step: { bar: [Function], foo: [Function] }
After: { bar: [Function], foo: [Function] }
SomeClass: { foo: [Function], bar: [Function] }
OtherClass: { foo: [Function], bar: [Function] }
Функция _filterProps имеет тенденцию сохранять некоторые значения в своем цикле «for..in», и я не могу сказать, почему. И я немного растерялся, потому что JavaScript не моя сильная сторона.
Версия моего узла v0.5.8-pre на Max OSX 10.6
Редактировать
Что ж, благодаря Заку Блуму и его способности видеть скрытые ошибки, я обнаружил, что забыл скопировать объект «Object.prototype», чтобы он ссылался на него.
Спасибо, JD