对不起,标题,但我不知道如何解释。

该函数采用URI,例如:/ foo / bar / 1293。如果存在该对象,则将其存储在类似于{foo:{bar:{1293:'content ...'}}}}的对象中。该函数遍历URI中的目录,并检查路径是否未定义,同时使用随后用eval()调用的代码构建字符串。包含代码的字符串看起来类似于delete memory [“ foo”] [“ bar”] [“ 1293”]

我还有其他方法可以做到这一点吗?也许将保存的内容存储在除
一个普通的物体?

remove : function(uri) {
        if(uri == '/') {
            this.flush();
            return true;
        }
        else {
            var parts = trimSlashes(uri).split('/'),
                memRef = memory,
                found = true,
                evalCode = 'delete memory';

            parts.forEach(function(dir, i) {
                if( memRef[dir] !== undefined ) {
                    memRef = memRef[dir];
                    evalCode += '["'+dir+'"]';
                }
                else {
                    found = false;
                    return false;
                }

                if(i == (parts.length - 1)) {
                    try {
                        eval( evalCode );
                    } catch(e) {
                        console.log(e);
                        found = false;
                    }
                }
            });

            return found;
        }
    }

最佳答案

这里不需要评估。像您一样向下钻取并最后删除属性:

parts.forEach(function(dir, i) {
    if( memRef[dir] !== undefined ) {
        if(i == (parts.length - 1)) {
            // delete it on the last iteration
            delete memRef[dir];
        } else {
            // drill down
            memRef = memRef[dir];
        }
    } else {
        found = false;
        return false;
    }
});

10-04 22:09
查看更多