为什么对象的引用被替换为其值

时间:2012-11-04 18:02:14

标签: javascript node.js session reference express

我使用node.js并表达。

我在'req.session'上保存了一个包含对象数组的复杂对象。 另外,我保存对数组中某个对象的引用。 例如:

var value = {
    name: "name"
    , values: []
};

req.session.value = value;

//
// I populate 'req.session.value' with values (with the same structure)
//

// then I save reference to one of the inner objects

var currentValue = req.session.value[3];

//
// later I try to change the save object
//

currentValue.name = "newName";

我希望如果我更改“currentValue”,那么“req.session.value[3]”也会更改。但是,出于某种原因,它不会发生。

具体来说,如果我在分配后立即更改currentValue,则req.session.value[3]会更改,但如果我在下一次调用中执行此操作,那么只需更改currentValue

在示例中:如果我更改“req.session”中app.get(...)的值,我会对“currentValue”中的“app.get(...)”进行分配它运行正常(两个地方的值都发生了变化),但是如果我在“app.post(...)”中更改它,则唯一的更改对象是currentValue,而req.session.value[3]则保持不变。 / p>

提前致谢,
Shai

代码:

'app.get("/template/:templateid/feature/add", isTemplate, function(req, res) {'
'    if (!req.session.features) { // if features empty'
''
'    // Save the first features level from the current template in the session '
'    req.session.features = req.session.template.feature;'
'    //'
'    if (!req.session.featureNodes) { // featureNotes is a stack/branch of the features'
'        req.session.featureNodes = [];'
'    }'
'    if (!req.query.featureroot || req.query.featureroot == "") {'
'    } else {'
'        var featureRoot = getFeature(req.query.featureroot, req.session.features); // get one object from req.session.features'
'        if (featureRoot) {'
'            req.session.featureNodes.push(featureRoot);     // save reference'
'            var featureR = req.session.featureNodes.pop();  // do check that work!'
'            var values = {'
'                name: "req.body.name"'
'                , description: "req.body.description"'
'                , wieght: "req.body.wieght"'
'                , created: new Date()'
'                , modified: new Date()'
'                , feature: []'
'            };'
''
'            featureR.feature.push(values); // also req.session.features changed'
'            req.session.featureNodes.push(featureRoot); // push the reference back for use later'
'        }      '
'    }'
'    res.render("addfeature2template.jade", { '
'        title: "Add new feature"'
'        ,template: req.session.template'
'        ,feature: req.session.featureNodes'
'    });'
'});'
''
'app.post("/feature/add", isUser, function(req, res) {'
'    var SUBMIT = "Create";'
'    var CANCEL = "Cancel";'
'    switch ( req.param("feature") ) {'
'        case SUBMIT:'
'            var fields = {  name: 1, description: 1, wieght: 1};'
'            var values = {'
'                name: req.body.name'
'                , description: req.body.description'
'                , wieght: req.body.wieght'
'                , created: new Date()'
'                , modified: new Date()'
'                , feature: []'
'            };'
'            if (req.session.featureNodes.length < 1) {'
'                req.session.features.push(values);'
'            } else {'
'                var featureRoot = req.session.featureNodes.pop(); // pop the reference'
'                featureRoot.feature.push(values);                 // change the object but the req.session.features didnt changed '
'            }'
'            req.session.template = template;'
'            res.redirect("/template/" + req.body.templateid);'
'        break;'
'        case CANCEL:'
'            res.redirect("/template/" + req.body.templateid);'
'            break;'
'    }'
'});'

1 个答案:

答案 0 :(得分:0)

req.session对象在请求之间被序列化(存储)。

示例:

请求1:

req.session = {};
var a = { hello : 'world' };
var b = a;
req.session.a = a;
req.session.b = b;

在此上下文中,变量abreq.session.areq.session.b指向一个对象。您可以在任何这些对象中更改字段hello,这将在每个对象中进行更改。

请求结束后req.session对象将被序列化为会话存储(memcached,mongodb等)。

请求2:

在请求之前2 req.session对象将从存储中反序列化。现在它包含没有引用的普通值。您可以访问req.session.areq.session.b,但现在可以访问两个不同的对象。