簡體   English   中英

Node.js在for..in循環中泄漏

[英]Node.js leaking for..in loops

我使用的是一個定制的對象處理庫(對於考試,不能使用我不親自編程的東西,即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和他看到隱藏的bug的能力,我發現我忘記復制“ Object.prototype”對象,因此它正在引用它。

謝謝JD

看起來您正在擴展Object.prototype,它將更改所有對象。

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

即使在循環語句中,您也必須聲明使用的變量,否則它們將成為全局變量。 例如更改諸如

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

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

編輯:實際上,您的問題是一個不同的問題,但是在您的代碼中仍然是一個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM