Javascript 对象值立即变为空
我有两个Javascript 对象值立即变为空,javascript,Javascript,我有两个console.log()调用的以下函数 get = function(path, obj){ for(var i = 0, path = path.split(/[\[\]\.]/), len = path.length; i < len; i++){ if(path[i]){ console.log(obj); obj = obj[path[i]]; console.log(obj)
console.log()
调用的以下函数
get = function(path, obj){
for(var i = 0, path = path.split(/[\[\]\.]/), len = path.length; i < len; i++){
if(path[i]){
console.log(obj);
obj = obj[path[i]];
console.log(obj);
if(typeof obj === 'undefined'){
return '';
}
}
}
return obj;
};
set = function(path, obj, dta){
for(var i = 0, path = path.split(/[\[\]\.]/), len = path.length; i < len; i++){
if(path[i]){
obj = obj[path[i]];
if(typeof obj === 'undefined'){
return false;
}
}
}
obj = dta;
return true;
};
调用第一个console.log()
时,对象如下所示:
var data = {
user: {
secret: null,
accessToken: null
},
baseurl: 'http://api.example.com'
};
set('user', data, {/* new object data */});
get('user', data);
{
baseurl: "http://api.example.com",
user: {
accessToken: "12324",
secret: "123413421341342134"
}
}
{
secret: null, accessToken: null
}
然后,当调用第二个console.log()
时,secret
和accessToken
如下所示:
var data = {
user: {
secret: null,
accessToken: null
},
baseurl: 'http://api.example.com'
};
set('user', data, {/* new object data */});
get('user', data);
{
baseurl: "http://api.example.com",
user: {
accessToken: "12324",
secret: "123413421341342134"
}
}
{
secret: null, accessToken: null
}
是什么导致这些值变为null?您的set()
函数不执行任何操作。将对象成员指定给变量,然后设置该变量,不会影响原始对象成员
您可以跟踪相关财产的父项,并使用该父项执行分配:
set = function(path, obj, dta){
var parent = obj;
var selector = null;
for(var i = 0, path = path.split(/[\[\]\.]/), len = path.length; i < len; i++){
if(path[i]){
parent = obj;
selector = path[i];
obj = obj[path[i]];
if(typeof obj === 'undefined'){
return false;
}
}
}
if (parent && selector)
{
parent[selector] = dta;
return true;
}
return false;
};
set=功能(路径、obj、dta){
var父代=obj;
var选择器=null;
对于(var i=0,path=path.split(/[\[\]\.]/),len=path.length;i
var get=function(路径,obj){
对于(var i=0,path=path.split(/[\[\]\.]/),len=path.length;i您的set()
函数不执行任何操作。将对象成员指定给变量,然后设置该变量,不会影响原始对象成员
您可以跟踪相关财产的父项,并使用该父项执行分配:
set = function(path, obj, dta){
var parent = obj;
var selector = null;
for(var i = 0, path = path.split(/[\[\]\.]/), len = path.length; i < len; i++){
if(path[i]){
parent = obj;
selector = path[i];
obj = obj[path[i]];
if(typeof obj === 'undefined'){
return false;
}
}
}
if (parent && selector)
{
parent[selector] = dta;
return true;
}
return false;
};
set=功能(路径、obj、dta){
var父代=obj;
var选择器=null;
对于(var i=0,path=path.split(/[\[\]\.]/),len=path.length;i
var get=function(路径,obj){
对于(var i=0,path=path.split(/[\[\]\.]/),len=path.length;i您的set()
函数不执行任何操作。将对象成员指定给变量,然后设置该变量,不会影响原始对象成员
您可以跟踪相关财产的父项,并使用该父项执行分配:
set = function(path, obj, dta){
var parent = obj;
var selector = null;
for(var i = 0, path = path.split(/[\[\]\.]/), len = path.length; i < len; i++){
if(path[i]){
parent = obj;
selector = path[i];
obj = obj[path[i]];
if(typeof obj === 'undefined'){
return false;
}
}
}
if (parent && selector)
{
parent[selector] = dta;
return true;
}
return false;
};
set=功能(路径、obj、dta){
var父代=obj;
var选择器=null;
对于(var i=0,path=path.split(/[\[\]\.]/),len=path.length;i
var get=function(路径,obj){
对于(var i=0,path=path.split(/[\[\]\.]/),len=path.length;i您的set()
函数不执行任何操作。将对象成员指定给变量,然后设置该变量,不会影响原始对象成员
您可以跟踪相关财产的父项,并使用该父项执行分配:
set = function(path, obj, dta){
var parent = obj;
var selector = null;
for(var i = 0, path = path.split(/[\[\]\.]/), len = path.length; i < len; i++){
if(path[i]){
parent = obj;
selector = path[i];
obj = obj[path[i]];
if(typeof obj === 'undefined'){
return false;
}
}
}
if (parent && selector)
{
parent[selector] = dta;
return true;
}
return false;
};
set=功能(路径、obj、dta){
var父代=obj;
var选择器=null;
对于(var i=0,path=path.split(/[\[\]\.]/),len=path.length;i