2016-11-29 63 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);