-2

I need to emulate in firefox the behavior of activeXobject in last versions of ie. I write addon with contentScript and want to implement to pages such variable. It is exactly what I want - It is a challenge in itself.

Example of IE 11 working code:

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

I need exactly same behavior in firefox with my variable For example what i 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, you can change the addon sdk or a browser source, change realization of all getters or something else

non
  • 1
  • 2
  • Based on your tags, are you building a Firefox add-on or just writing a web page? – apsillers Jul 17 '15 at 16:26
  • building firefox addon – non Jul 17 '15 at 16:32
  • I don't understand your last example. You're saying, you want `window.ActiveXObject` to be `undefined`. but you want `new window.ActiveXObject` to work? If so, you need a different way to solve your real problem because this isn't possible. Also, please read about the [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) (asking questions about your solution rather than describing your fundamental problem) because that's what you're doing here and it is not helping you find an answer. – jfriend00 Jul 17 '15 at 16:41
  • my last example work in IE-11, yes it work not by javascript rules but browser architecture and that allow to declare variables this way, @Bergi showed how it can work – non Jul 17 '15 at 16:48
  • What code needs to access the variable? In other words, does it need to be accessed by external code, or do you just want a variable that can be accessed globally by all of your code without being attached to the window? – Ryan Neuffer Jul 17 '15 at 16:52
  • @apsillers, fixed examples, just I think it lies in the same plane – non Jul 17 '15 at 17:03
  • @Ryan Neuffer I just want a variable that can be accessed globally by all of your code without being attached to the window, mainly I need that window['my variable'] return undefined. – non Jul 17 '15 at 17:03
  • @non - as we have said multiple times, in the browser, that does not exist. All global variables are properties of the `window` object in a browser. That's how it works. You can't change it. You will need to find a different solution to whatever problem you are actually trying to solve. – jfriend00 Jul 17 '15 at 17:29
  • So, now you're asking how to edit the source code of an open source browser to change how global variables work in Javascript? If that's really what you're asking, then I'd suggest you create a new question that asks that specific question though I rather doubt you will find many or any people here who know how to do that either. – jfriend00 Jul 17 '15 at 17:40
  • @jfriend00 i writed about source and sdk since start, and here no tags for firefox dev except sdk one. Anyway i give up on stackoverflow, ill do something by myself – non Jul 17 '15 at 17:42

3 Answers3

2

In browser Javascript, there is no such thing as a global variable that is not a property of the window object. So, if that's truly what you are trying to do, then it cannot be done. Yes, you might be able to create a getter on the window object with Object.defineProperty(), but that's still a property on the window object so I'm not sure how that helps you.

Likewise, there is no structure in Javascript such that:

window.ActiveXObject === undefined

and this works:

var x = new window.ActiveXObject;

Javascript just simply doesn't work that way. The property either exists or it doesn't. It can't be undefined for one way of accessing it and defined for some other way of accessing it.


You might also be able to create the property on the window object so it is not enumerable if you want it to be less visible for some reason.

If you explained what you're really trying to accomplish, there may be some work-arounds by enclosing the relevant code in a closure and defining the variable in the closure, but without knowing more details about what the actual problem to be solved is, we can't help more specifically than that.


If you just want the variable defined for YOUR code only, then you can just put it in a closure:

(function() {
    var myVar = "foo";

    // other parts of your code


    // elsewhere in your code
    console.log(myVar);
})();

Your code can treat it like a global, but it's really not a global, it's just defined within the scope that your code lives and not available to any other code outside this scope.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Your answer is spot-on correct, but isn't it also true that the notional ECMAScript *references* created by `window.variable` and by `variable` will differ? (One has the `window` object as its base value, one has an environment record.) I'm not sure that there's any way to leverage that, though, even in ES6. Maybe the Firefox add-on API can, though? – apsillers Jul 17 '15 at 16:36
  • i added another examples what i want to get, it is not necessary to reach the goal but is a challenge in itself – non Jul 17 '15 at 16:39
  • @apsillers - Yes, there is a microscopic difference between something declared as `var x = "foo"` and something declared as `window.x = "foo"`. In the latter, you can use `delete` to remove the property from the `window` object. In the former, you cannot (so obviously the runtime knows the difference). But, I don't know how that actually helps the OP because both are available for reading as `window.x`. – jfriend00 Jul 17 '15 at 16:39
  • @non - Since you've edited your question some, I added some more to the first part of my answer and added a closure example to the end of my answer. – jfriend00 Jul 17 '15 at 17:24
  • @jfriend00 I suggested the closure, but he said it wouldn't work for his application. Based on that, my assumption is that the variable needs to be accessed globally and not just within the scope of a project containing closure. – Ryan Neuffer Jul 17 '15 at 17:30
  • @RyanNeuffer - yeah, it's completely unclear what the real problem is that the OP is trying to solve and they insist on something that doesn't exist in browser Javascript. So, I think we're done. – jfriend00 Jul 17 '15 at 17:43
0

Theoretically, this is possible with lexically declared variables in ES6:

let variable = 5;
console.log(variable); // 5
console.log(window.variable) // undefined

However, the current let support in FF is not as advanced, and still treats this a property of the global object.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • great solution, unfortunally as you say it is not work in firefox now and just declare variable in window. – non Jul 17 '15 at 16:49
  • But hey, you say you can edit the browser source, so you may want to try pushing ES6 support, or check some nightly builds :-) – Bergi Jul 17 '15 at 17:41
  • Im not mozilla's developer and can change only small not important things. I can not even find a __defineGetter__ function declaration in the source – non Jul 17 '15 at 17:46
0

If the variable only needs to be accessible by your project code, wrap all of your project code in a self executing anonymous function.

(function(){

    var internalGlobalVar = "some value";

    //All your code gets wrapped in here...

})();

Then if you tried to alert(window.internalGlobalVar) you would get undefined.

We use google closure compiler with plovr and wrap all of our code in a self executing anonymous function so basically all of our global variables are project scope, not on the window "output-wrapper": "(function($){%output%})(jQuery);"

Ryan Neuffer
  • 782
  • 4
  • 8
  • i need universal way to set it on any page by content script of my addon – non Jul 17 '15 at 17:14
  • So it DOES need to be accessed by external code. My answer is a universal way to create project scope variables that are not accessible on the window (or exposed to external code). If it needs to be a true global accessible by external code then as @jfriend00 it will be a property of the window. – Ryan Neuffer Jul 17 '15 at 17:25