angular: Object.prototype.toString() is overridden and behaves incorrectly

After updating to version 0.9, the Object.prototype.toString.call( undefined ) returns [Object Window] instead of [Object Undefined] when the strict mode isn’t turned on.

The root of this bug is caused by this line:

https://github.com/angular/zone.js/pull/734/files#diff-7cf54420c5bf443f7d3575ad72ac1de3R44

where this refers to the Window in non-strict mode.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 8
  • Comments: 24 (8 by maintainers)

Most upvoted comments

When this fix will be released through npm ?

I found two workarounds:

1- Add the .js to the import in the pollyfills.ts in the Angular app:

image

2- Disable the toString patching as specified in:

https://github.com/angular/zone.js/blob/master/MODULE.md

I added the following to the pollyfills.ts in the Angular app:

__Zone_disable_toString = true; // Zone will not patch Function.prototype.toString image

This is not a zone.js issue, you can disable the monkey patch of zone.js for toString() by define (window as any).__Zone_disable_toString = true in a file called zone-flags.ts and then import this file before importing zone.js

import './zone-flags.ts';
import 'zone.js/dist/zone';

After this, the error still exists, the reason is

export class WindowWrapper extends Window {
}

in IE, the base class Window is not a function, instead a Object, so this issue can be reproduced by the following code.

<html>
  <body>
    <script>
      const w = Window;
      console.log(typeof w);
    </script>
  </body>
</html>

in Chrome, the type will be function, and in IE it will be Object, so the error happened, so we need some other tricks to extends Window in IE, but still this has nothing to do with zone.js, I will do some research later about this issue, but will close this one for now.

@ma2ciek, @cristhiank , thanks for posting the issue and providing the information, I will check it.

@cristhiank, thanks, could you try the latest version on master branch https://github.com/angular/zone.js/blob/master/dist/zone-evergreen.js to test again?