Jeremy David
Jeremy David

Reputation: 246

Node.js leaking for..in loops

I was using a custom made object handling library (for exams, can't use things that I don't program personnaly, ie JS.class) which tends to break scope of functions.

File 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;
}

File 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);

Output of "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] }

The _filterProps function tends to retain some values in its "for..in" loop and I can't tell why. And I'm a little lost because javascript is not my forte.

My node version is v0.5.8-pre on Max OSX 10.6

Edit

Well thanks to Zack Bloom and his ability to see hidden bugs, I discovered that I forgot to copy the "Object.prototype" object so it was referencing it.

Thanks, JD

Upvotes: 1

Views: 483

Answers (2)

thejh
thejh

Reputation: 45568

Even in a loop statement, you have to declare variables you use or they'll become globals. E.g. change things like

for(prop in arg) { obj_out[prop] = arg[prop]; }

to something like

for(var prop in arg) { obj_out[prop] = arg[prop]; }

Edit: Actually, your problem is a different one, but it's still an issue in your code.

Upvotes: 2

Zack Bloom
Zack Bloom

Reputation: 8417

It looks like you are extending Object.prototype, which will change all objects.

base = Object.prototype
cls.prototype = base
_add(cls.prototype, ...)

Upvotes: 5

Related Questions