2016-11-29 97 views
1

我一直在寻找一种方法来做到这一点,但似乎无法找到任何东西,我有不同的配置对象,我需要将其保存为变量中的文本处理以后,这里是一个示例:JavaScript - 将方法保存为字符串

对象:

args.config.config = { 
     next: null, 
     final:[], 
     delimiter: '~', header: false, 
     step: function (row) { 
      var item = { 
       'line_code': row.data[0][0], 
       'order': row.data[0][1] 
      } 
      args.config.config.final.push(item); 
     }, 
     complete: function (result) { 
      console.log('Reading data completed. Processing.'); 
      return args.config.config.next(null, args.config.config.final); 
     }, 
     error: function() { 
      console.log('There was an error parsing'); 
     }' 
    } 

我需要这个保存为一个字符串,所以是这样的:

args.config.config = "{object goes here}"; 

没有把所有东西都放在一个巨型的线上或者添加断行字符,因为这会在稍后被解析以用于配置中,并且这会弄乱一切,任何想法?

UPDATE: 所以他们改变成文本未必是最好的解决方案,这些CONFIGS将被存储在一个数据库蒙哥,所以它可以把它们看成是(我还没有尝试过呢)。

一个我也陷入了另一个问题是,在配置对象我有这样的:

final.push(item) 

return next(null, final) 
将在另一个文件中使用的配置对象定义

其他文件:

exports.parse = function(args, next){//next is what I need to call in the config 

    var final = []; //this is the final referred to in the config object 
    .... 
    Baby.parse(data, args.config) 
} 

所以return next(null,final)和final.push(result)必须引用新文件中的var/function,但我不知道如何让它工作,不过为什么我不得不添加的配置对象和一个空的下一个功能的最后一个数组,然后分配给它像这样:

exports.parse = function(args, next){ 
    args.config.next = next; 
    .... 
    Baby.parse(data, args.config) 
} 

对象与丑陋行调用它:

return args.config.config.next(null, args.config.config.final); 

如果任何人有绕过这个方法,这将是非常感激。

+0

为什么对象必须是文本,以便储存供日后使用? –

+0

你不能因为这些功能...你可以说在另一个对象的字符串/数字,并使该功能使用这些值? – epascarello

+0

你想要做的是“序列化”。有整个库专门用它们的方法序列化和反序列化对象。最常见的方法是将对象定义为类的实例,将类名称存储为序列化的一部分,然后在反序列化中重构类的实例,可能使用可以实例化的“类工厂”的概念一个基于类名的对象及其方法,来自数据。 – 2016-11-29 21:06:30

回答

2

如果使用JSON.stringify with a "replacer" functionJSON.parse with a "reviver" functionnew Function()一起,你可以做到这一点:

我不知道,我在第二次(更新)问你。一旦对象被解析回对象中,为什么不能在调用任何对象的方法之前将nextfinal属性初始化为有效对象?您甚至可以在该方法中添加测试,在返回任何内容之前检查是否存在finalnext

var myObj = { 
 
     next: null, 
 
     final:[], 
 
     delimiter: '~', 
 
     header: false, 
 
     step: function (row) { 
 
      var item = { 
 
       'line_code': row.data[0][0], 
 
       'order': row.data[0][1] 
 
      }; 
 
      args.config.config.final.push(item); 
 
     }, 
 
     complete: function (result) { 
 
      console.log('Reading data completed. Processing.'); 
 
      return args.config.config.next(null, args.config.config.final); 
 
     }, 
 
     error: function() { 
 
      console.log('There was an error parsing'); 
 
     } 
 
    }; 
 

 
// Stringify the object using a replacer function that will explicitly 
 
// turn functions into strings 
 
var myObjString = JSON.stringify(myObj, function(key, val) { 
 
     return (typeof val === 'function') ? '' + val : val; 
 
}); 
 

 
// Now, parse back into an object with a reviver function to 
 
// test for function values and create new functions from them: 
 
var obj = JSON.parse(myObjString, function(key, val){ 
 
    
 
    // Make sure the current value is not null (is a string) 
 
    // and that the first characters are "function" 
 
    if(typeof val === "string" && val.indexOf('function') === 0){ 
 

 
     // Isolate the argument names list 
 
     var start = val.indexOf("(") + 1; 
 
     var end = val.indexOf(")");  
 
     var argListString = val.substring(start,end).split(","); 
 
     
 
     // Isolate the body of the function 
 
     var body = val.substr(val.indexOf("{"), val.length - end + 1); 
 
     
 
     // Construct a new function using the argument names and body 
 
     // stored in the string: 
 
     return new Function(argListString, body); 
 
     
 
    } else { 
 
     // Non-function property, just return the value 
 
     return val; 
 
    } 
 
    } 
 
); 
 

 
// Test the method: 
 
obj.error(); // 'There was an error parsing' is written to console. 
 

 
// Examine the object: 
 
console.log(obj);