javascript - Declare variable so that would be it visible in global context but not belong to window or gett -


i need emulate in firefox behavior of activexobject in last versions of ie. write addon contentscript , want implement pages such variable. want - challenge in itself.

example of ie 11 working code:

if (window.activexobject == undefined) {     var = new window.activexobject; // [activexobject object] } 

i need same behavior in firefox variable example want

<script>     console.log(window.variable) // undefined     console.log(new window.variable); // [object variable]     console.log(new variable) // [object variable] if not possible previous string </script> 

for solving, can change addon sdk or browser source, change realization of getters or else

in browser javascript, there no such thing global variable not property of window object. so, if that's trying do, cannot done. yes, might able create getter on window object object.defineproperty(), that's still property on window object i'm not sure how helps you.

likewise, there no structure in javascript such that:

window.activexobject === undefined 

and works:

var x = new window.activexobject; 

javascript doesn't work way. property either exists or doesn't. can't undefined 1 way of accessing , defined other way of accessing it.


you might able create property on window object not enumerable if want less visible reason.

if explained you're trying accomplish, there may work-arounds enclosing relevant code in closure , defining variable in closure, without knowing more details actual problem solved is, can't more that.


if want variable defined code only, can put in closure:

(function() {     var myvar = "foo";      // other parts of code       // elsewhere in code     console.log(myvar); })(); 

your code can treat global, it's not global, it's defined within scope code lives , not available other code outside scope.


Comments