Javascript - stub/substitute of object

78 Views Asked by At

In GWT framework there is a class JsoSplittable, which contains following code ($wnd is reference to global window object):

  return $wnd.JSON && $wnd.JSON.stringify && $wnd.JSON.stringify({
    b : function() {
    }
  }) == '{}';

I'm using this code in HTML5 web worker, which doesn't have access to window (therefore global $wnd can be redefined). How can I create in pure Javascript custom $wnd object which will satisfy condition in snippet above?

True can be hardcoded, I will force modern browser anyway.

PS: patch to GWT will be submitted, but I also need workaround for legacy environment

1

There are 1 best solutions below

0
On BEST ANSWER

I think this is your easiest bet

$wnd = {JSON: JSON};

If you don't have access to JSON, you could stub that out too

$wnd = {JSON: {stringify: function() { return '{}'; }}};

$wnd.JSON && $wnd.JSON.stringify && $wnd.JSON.stringify({b : function() {}}) == '{}';
// true