2016-10-05 2 views
0

자바 스크립트에서 변수를 마샬링해야 나중에 다시 언 마샬링합니다.uneven을위한 크로스 브라우저 교체

JSON을 사용해 보았습니다. JSON은 number, string, boolean, null, array 및 object에 대해서만 작동합니다. 그러나 내가 처리하고자하는 변수는 RegExp 인스턴스이거나 함수 일 수도 있습니다.

나는 uneval 내가 실제로 원하는 것을 찾았습니다. 그러나 모든 브라우저에서 작동하지 않을 수도 있습니다.

자바 스크립트 변수를 마샬링 및 언 마샬링 할 수있는 크로스 브라우저 솔루션을 찾고 싶습니다.

+0

당신이 문자열로 저장해야 regexps '에 대한 X/Y의 문제 – adeneo

+0

같은 소리하고 할'새 정규식 (regexpString)'. – Maxx

+0

함수를 문자열로 저장 한 다음,'new Function (arg1, functionString)'함수를 저장할 수 있습니다. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function – Maxx

답변

0

다른 구현을 검색했지만 아무도 저에게 적합하지 않습니다. 그래서 현재 내가하는 일은 implementing another un_eval function myself ...

// 더 나은 해결책이 있기를 바랍니다.

var un_eval = (function() { 
    var helper = function (obj, seen) { 
    try { 
     if (obj === null) return 'null'; // null 
     if (obj === void 0) return '(void 0)'; // undefined 
     if (obj == null) return '({})'; // maybe undetectable 
     if (typeof obj === 'number') { 
     if (1/obj === -Infinity) return '-0'; 
     // toString should work all values but not -0 
     return Number.prototype.toString.call(obj); 
     } 
     // string or boolean 
     if (!(obj instanceof Object)) return JSON.stringify(obj); 
     // String, Number, Boolean 
     if (obj instanceof String) return '(new String(' + helper(String.prototype.valueOf.call(obj)) + '))'; 
     if (obj instanceof Number) return '(new Number(' + helper(Number.prototype.valueOf.call(obj)) + '))'; 
     if (obj instanceof Boolean) return '(new Boolean(' + helper(Boolean.prototype.valueOf.call(obj)) + '))'; 
     // RegExp; toString should work 
     if (obj instanceof RegExp) return RegExp.prototype.toString.call(obj); 
     // Date; convert obj to Number should work 
     if (obj instanceof Date) return '(new Date(' + helper(Number(obj)) + '))'; 
     // Function 
     if (obj instanceof Function) { 
     var func = Function.prototype.toString.call(obj); 
     if (/\{\s*\[native code\]\s*\}\s*$/.test(func)) return null; 
     return '(' + func + ')'; 
     } 
     if (seen.indexOf(obj) !== -1) return '({})'; 
     var newSeen = seen.concat([obj]); 
     // Array 
     if (obj instanceof Array) { 
     var array = obj.map(function (o) { return helper(o, newSeen); }); 
     // Add a comma at end if last element is a hole 
     var lastHole = array.length && !((array.length - 1) in array); 
     return '[' + array.join(', ') + (lastHole ? ',' : '') + ']'; 
     } 
     // Object 
     if (obj instanceof Object) { 
     var pairs = []; 
     for (var key in obj) { 
      pairs.push(JSON.stringify(key) + ':' + helper(obj[key], newSeen)); 
     } 
     return '({' + pairs.join(', ') + '})'; 
     } 
     return '({})'; 
    } catch (_ignore1) { } 
    // there should be something wrong; maybe obj is a Proxy 
    try { 
     if (obj instanceof Object) return '({})'; 
     else return 'null'; 
    } catch (_ignore2) { } 
    // there really should be something wrong which cannot be handled 
    return 'null'; 
    }; 
    return function un_eval(obj) { 
    return helper(obj, []); 
    }; 
}());