We've already talked about contexts and how to pass objects between the window and Node.js contexts using global
, but there are still a few things we need to consider when developing on multiple contexts.
As first, you should consider that switching between the DOM and the Node.js context takes a while, so if you're planning to run hundreds of concurrent operations that depend on both the window and Node.js contexts, you might get a slight delay. In order to avoid this kind of issue, you'd better choose in advance which context to adopt.
One more thing that might be very confusing at first is that the same objects from different contexts also have different constructors in their prototype chain. That results in misbehavior of the instanceof
operator, which will not be able to recognize prototypes coming from a different context.
Let's check an example:
nodeContext.js:
exports.myArray = ['one','two','three'];
index.html:
var windowContext = { myArray: ['one','two','three'...