【问题标题】:Node.js leaking for..in loopsNode.js 泄漏 for..in 循环
【发布时间】:2011-10-13 18:06:34
【问题描述】:

我使用的是定制的对象处理库(对于考试,不能使用我不亲自编程的东西,即 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 不是我的强项。

我的节点版本是 Max OSX 10.6 上的 v0.5.8-pre

编辑

感谢 Zack Bloom 和他发现隐藏错误的能力,我发现我忘记复制“Object.prototype”对象,所以它正在引用它。

谢谢,JD

【问题讨论】:

标签: javascript node.js for-loop


【解决方案1】:

看起来您正在扩展 Object.prototype,这将更改所有对象。

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

【讨论】:

  • 嗯 FFFFFFUUUUUUUUU 我忘了复制对象,我真是太愚蠢了。马上试试!
【解决方案2】:

即使在循环语句中,您也必须声明您使用的变量,否则它们将成为全局变量。例如。改变像

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

类似

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

编辑:实际上,您的问题是另一个问题,但它仍然是您的代码中的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-12
    • 2017-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-24
    • 2011-07-08
    • 1970-01-01
    相关资源
    最近更新 更多