Fix isssues #508 #511

By using `self` (and detecting the presence of it for NodeJS's benefit), the problem of detecting the global scope in Web Workers and in regular browser environments go away.

See
* https://developer.mozilla.org/en-US/docs/Web/API/Window.self
* http://www.w3.org/TR/workers/#the-global-scope

This fixes #508 and #511