2016-11-29 6 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}"; 

これは後で構成で使用されるように解析されるので、1つの巨大な行にすべてを置いたり、改行文字を追加したりすることはありません。

UPDATE: だから最善の解決策ではないかもしれないテキストにそれらを変更し、これらのconfigsはモンゴデータベースに保存されますので、(私はまだそれを試していない)であるとして、それはそれらを取ることがあります。

final.push(item) 

とconfigオブジェクトを使用して別のファイルに定義されます

return next(null, final) 

:私はに実行していた他の問題の

一つは、configオブジェクトに私はこれを持っていたということでした。

他のファイル:

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を参照する必要がありますが、それを動作させる方法はわかりません。そのようにそれを割り当てた後、configオブジェクトとヌル次の機能で、最終的な配列を追加する必要がありました:誰もが持っている場合

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

:オブジェクトが醜いラインでそれを呼んでいた

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

この周りに、それは非常に高く評価されるだろう。

+0

オブジェクトは、後のために保存するために、テキストである必要がないのはなぜ? –

+0

関数のためにすることはできません...他のオブジェクトの文字列/数値を言うことができますし、関数がそれらの値を使用することができますか? – epascarello

+0

あなたがしたいことの言葉は "シリアライズ"です。それらのメソッドでオブジェクトをシリアライズおよびデシリアライズするためのライブラリ全体があります。最も一般的なアプローチは、オブジェクトをクラスのインスタンスとして定義し、クラス名をシリアライゼーションの一部として格納し、次にデシリアライズでクラスのインスタンスを再構成します。インスタンスを生成できる「クラスファクトリ」という概念を使用しますそのクラス名に基づいたオブジェクトとそのメソッドをデータから取得します。 –

答えて

2

あなたはnew Function()とともにJSON.stringify with a "replacer" functionJSON.parse with a "reviver" 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);

関連する問題