我有一个主要的javascript对象(这个数组我在服务器端的php脚本中放入json_encode函数):
array(
'priceManager' => array(
'array' => array(
'log' => array(
'options' => array(
'path' => 'local'
)
),
'settings' => array(
'options' => array(
'path' => 'local'
)
),
'articles' => array(
'options' => array(
'path' => 'global',
'lifetime' => '0:00:15'
)
),
'currencies' => array(
'options' => array(
'path' => 'global',
'lifetime' => '0:00:15'
)
)
),
'item' => array(
'dbConnect' => array(
'options' => array(
'hostName' => '***',
'userName' => '***',
'password' => '***',
'databaseName' => '***'
)
),
'getPrice' => array(
'options' => array(
'curr' => 'RUB',
'method' => 'floor',
'accuracy' => 5,
'ratio' => array(
'all' => 1.1,
'001' => 1.2,
'0012' => 1.5
)
)
),
'getExchange' => array(
'options' => array(
'method' => 'floor',
'accuracy' => 5
)
),
'getRow' => array(
'options' => array(
'template' => array(
array('function' => 'getName'), '!', array('function' => 'getPrice', 'params' => 'EUR'), '!', array('function' => 'getCurrency')
)
)
)
)
)
);
在加载文档后,我将其拆分为几个较小的对象(通过'options'键):
'options' => array(
'path' => 'local'
)
'options' => array(
'path' => 'local'
)
'options' => array(
'path' => 'global',
'lifetime' => '0:00:15'
)
'options' => array(
'path' => 'global',
'lifetime' => '0:00:15'
)
'options' => array(
'hostName' => '***',
'userName' => '***',
'password' => '***',
'databaseName' => '***'
)
'options' => array(
'curr' => 'RUB',
'method' => 'floor',
'accuracy' => 5,
'ratio' => array(
'all' => 1.1,
'001' => 1.2,
'0012' => 1.5
)
)
'options' => array(
'method' => 'floor',
'accuracy' => 5
)
'options' => array(
'template' => array(
array('function' => 'getName'), '!', array('function' => 'getPrice', 'params' => 'EUR'), '!', array('function' => 'getCurrency')
)
)
因此,当我更改子对象中的值时,父对象会立即更改。但是当我更新父对象时,子对象会丢失它们的引用。我试图重新分配父对象的值(因为原子值按值设置),但它什么都没有:
this.reassignment = function(t, d){
var that = this;
$.each(d, function(attr, val){
if(typeof(val) === 'object'){
that.reassignment(t[attr],d[attr]);
}else{
if(t[attr] === null){
t[attr] = {};
}
console.log(val);
t[attr] = val;
}
});
};
问题:
如何在重新分配后保存对父对象的引用?
我的整个剧本(一种图书馆)太大了,无法在这里发布。完整版http://coding-for-food.com/priceManager/jPanel.js
使用该库的页面http://coding-for-food.com/priceManager/adm.php
创建父对象 - 第158行(准备方法)
创建子对象 - 第254行(拆分方法)
问题方法 - 第648行(重置方法)。