eval - make javascript literal inheriting from different prototype -


i have code goes against practice. however, not want comments on - purely academic.

<html> <head> <script> function run() {     var fakecontext = {          array : fr.contentwindow.array || fr.array; //another context/window array     }      fakecontext.array.prototype.remove = function(el) {/*some code*/};      (fakecontext) {         var somecode = "var x = ['1', '2']; x.remove('2')";          eval(somecode);     } } </script> </head> <body> <iframe src="about:blank" name="fr"></iframe> </body> </html> 

this array created when evaluating somecode inherits top level array in code runs instead of inheriting fakecontext.array. meaning array x not have prototype function .remove()

how can (if there way) literals in somecode-string inherit fakecontexts array.prototype?

the problem array literal [] not evaluate in same way new array(). first create native array object, while second checks scope array variable , executes constructor.

var fakewin = fr.contentwindow || fr; var fakecontext = {array: fakewin.array};  (fakecontext) {     array.prototype.remove = function(el) {/*some code*/};     eval("var x = new array('1', '2'); x.remove('2')"); } 

to create arrays prototype different context, need eval literals in fake window environment:

var fakewin = fr.contentwindow || fr;  (fakewin) {     array.prototype.remove = function(el) {/*some code*/};     eval("var x = new array('1', '2'); x.remove('2')"); }  // or:  fakewin.array.prototype.remove = function(el) {/*some code*/}; fakewin.eval("var x = new array('1', '2'); x.remove('2')"); 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -