Possible substitution of designers in the values generated by simple constructs {} and [] in JavaScript?

As you know, in real application the JavaScript global object is not unique: each browser window has its own global object and its own system global constructors, which are its fields.


Typically, this is useful because the use of the library Prototype or other means, to replenish or substitute for the prototype, does not affect similar objects in other Windows. Bugs do not spread too wide (multiple Windows), villainous applications do not have the ability to implement a confidential data structure, and so on.


However, this separation has its price, and often it might seem unnecessary to. In particular, a simple check value instanceof Array is not able to suffice to check whether a passed value is an array, if it was transferred from another context in which a different value Array. (More information can be found in the article "Determining with absolute accuracy whether or not ...", for example.)


One way to overcome this drawback is to create special features such as a method of Array.isArrayintroduced in ECMAScript 5. This method, however, does not help in changing the behavior of those source code libraries that were previously written without taking into account possible difference of contexts and without the use of the new functions in ECMAScript.


The second way (perhaps more obvious) is to replace the global object in one context to the global object in another context. Something like this:

  • Object = otherContext.Object;

     
  • Array = otherContext.Array;

     
  • and so on...


Unfortunately, we have to see that the equivalence ({}).constructor === Objectand [].constructor === Array after such replacement is broken.


Is there a way to restore them to simple designs {...} and [...] continued to work, but the generated objects and arrays of borrowed context?
October 3rd 19 at 04:32
2 answers
October 3rd 19 at 04:34
Solution
October 3rd 19 at 04:36
Indirectly on the topic:
If you are forwarding data to the frame via postMessage, the problem with x instanceof RegExp/Array should not be
// top
frames.myframe.postMessage([ 'action', [], /x/ ], '*');

// myframe
window.onmessage = function(event){
 event.data[1] instanceof Array // -> true
 event.data[2] instanceof RegExp // -> true
}

Find more questions by tags JavaScriptLayouts and prototypes