本文介绍了在Internet Explorer中实现Mozilla的toSource()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有人为Internet Explorer和其他非Gecko浏览器实现了Mozilla的Object.toSource()方法?我正在寻找一种将简单对象序列化为字符串的轻量级方法。

Has anyone implemented Mozilla's Object.toSource() method for Internet Explorer and other non-Gecko browsers? I'm looking for a lightweight way to serialize simple objects into strings.

推荐答案

请考虑以下事项:(使用FireFox 3.6时)

Consider the following: (when using FireFox 3.6)

javascript:
  x=function(){alert('caveat compter')};
  alert(['JSON:\t',JSON.stringify(x),'\n\ntoSource():\t',x.toSource()].join(''));

显示:

toSource():( function(){alert(caveat compter);})

toSource(): (function () {alert("caveat compter");})

甚至:

javascript:
x=[];x[3]=x;
alert('toSource():\t'+x.toSource());
alert('JSON can not handle this at all and goes "infinite".');
alert('JSON:\n'+JSON.stringify(x));

显示:

和去'无限'消息来自JSON的stackoverflow递归离题。

and the "going 'infinite'" message whence follows JSON's stackoverflow recursive digression.

这些示例强调了由toSource()呈现的JSON表示中明确排除的表达式的微妙之处。

The examples emphasize the subtleties of expression explicitly excluded from JSON representation that are rendered by toSource().

对于所有情况,组合程序来复制相同的结果并不容易,因为Gecko toSource()原语非常强大。

It is not easy to compose a program to replicate the same results, for ALL cases, as the Gecko toSource() primitive, which is exceptionally powerful.

下面是一些复制toSource()功能的程序必须成功处理的移动目标:

Below are a few of the 'moving targets' that a program duplicating toSource() functionality MUST handle successfully:

javascript:
function render(title,src){ (function(objRA){
    alert([ title, src,
        '\ntoSource():',objRA.toSource(),
        '\nJSON:',JSON.stringify(objRA)     ].join('\n'));
    })(eval(src));
}
render('Simple Raw Object source code:',
    '[new Array, new Object, new Number, new String, ' +
        'new Boolean, new Date, new RegExp, new Function]'  );

render( 'Literal Instances source code:',
    '[ [], 1, true, {}, "", /./, new Date(), function(){} ]'    );

render( 'some predefined entities:',
    '[JSON, Math, null, Infinity, NaN, ' +
        'void(0), Function, Array, Object, undefined]'      );

显示:


    Simple Raw Object source code:
    [new Array, new Object, new Number, new String,
                new Boolean, new Date, new RegExp, new Function]

    toSource():
    [[], {}, (new Number(0)), (new String("")),
                (new Boolean(false)), (new Date(1302637995772)), /(?:)/,
                            (function anonymous() {})]

    JSON:
    [[],{},0,"",false,"2011-04-12T19:53:15.772Z",{},null]

然后显示:


    Literal Instances source code:
    [ [], 1, true, {}, "", /./, new Date(), function(){} ]

    toSource():
    [[], 1, true, {}, "", /./, (new Date(1302638514097)), (function () {})]

    JSON:
    [[],1,true,{},"",{},"2011-04-12T20:01:54.097Z",null]

最后:


    some predefined entities:
    [JSON, Math, null, Infinity, NaN, void(0),
                        Function, Array, Object, undefined]

    toSource():
    [JSON, Math, null, Infinity, NaN, (void 0),
        function Function() {[native code]}, function Array() {[native code]},
            function Object() {[native code]}, (void 0)]

    JSON:
    [{},{},null,null,null,null,null,null,null,null]

如果翻译是待用或不太严格,如果需要简单的良性人类消费来查看对象的内部。作为表示,主JSON功能是在环境之间传输某些结构化信息。

The previous analysis is significant if the translations are 'to be used' or less stringent if the need is for simple benign human consumption to view an object's internals. A primary JSON feature, as a representation, is the transfer of some structured information 'to be used' between environments.

toSource()函数的质量是一个因素在一个程序的指称语义中,影响但不限于:

往返计算,最小不动点属性和反函数。

The quality of a toSource() function is a factor in the denotational semantics of a programme influencing, but not limited to:
round trip computations, least fixed point properties, and inverse functions.


  • 重复代码转换
    停顿到静态状态?

  • obj.toSource()==
    eval(eval(eval) (obj.toSource())。toSource())。toSource())。toSource()?

  • 考虑
    是否有意义obj == eval(obj) .toSource())?

  • 撤消转换结果,而不是
    只是一个类似的对象,但
    IDENTICAL一个?

    这是一个加载的
    问题,在克隆操作对象时会产生深刻的影响

  • Does repetition of code conversionquiesce to a static state?
  • Does obj.toSource() ==eval(eval(eval(obj.toSource()).toSource()).toSource()).toSource()?
  • Does it make sense to considerwhether obj == eval(obj.toSource())?
  • Does undoing a conversion result in, notjust a similar object, but anIDENTICAL one?
    This is a loadedquestion with profound implicationswhen cloning an operational object.

和很多很多更多...

and many, many more ...

请注意,上述问题会带来更多重要意义当obj包含已执行的代码对象时,例如(new Function ...)()!

Note that the above questions take on added significance when obj contains an executed code object, such as (new Function ... )()!

这篇关于在Internet Explorer中实现Mozilla的toSource()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 12:07