📜 ⬆️ ⬇️

Handling indefinite global variables is far from common sense! But it can be overcome ...

The web developer’s common sense suggests that in browser javascript, global variables are properties of the window object - so window.White and just something should be synonymous and behave the same way (unless, of course, we are in any such function where the variable of something has been redefined locally).

The web developer’s common sense also suggests that an undefined variable should have the value undefined - so that nibud and undefined should be synonymous and behave the same when the nibus variable was not defined in javascript (and if, of course, no chud did not dare to override undefined ).

But if you are going to program for Opera 11 (whether it is the initial version of Opera 11.01 or the newer Opera 11.61), then be prepared to renounce common sense in both cases! (Not only in Opera, but also in other browsers and environments - but more on that later.)
')
To see this clearly, let's use the Underscore.js library, which just has a convenient function for testing undefined variables - this is the _.isUndefined () function, in the source code defined in a self-evident way:

// Is a given variable undefined? _.isUndefined = function(obj) { return obj === void 0; }; 

And since the Underscore.js library is connected to its own page http://documentcloud.github.com/underscore/ , I suggest simply accessing this page and running Opera Dragonfly with the well-known keyboard shortcut Ctrl + Shift + I.

It is enough to make three tests in the Dragonfly console to immediately touch the chthonic depths of the lively horror:

[test results]

What do you see in this screenshot?

First I checked _.isUndefined (undefined) and got true - of course, so it should be.

Then I checked _.isUndefined (whatNibbe) - and this caused an error in the Opera! We see, therefore, that undefined behaves not at all in the same way as an ordinary indefinite variable. We also see something darker: Opera does not know how to accurately pass undefined global variables (except the special undefined variable undefined ) inside functions!

And finally, I checked _.isUndefined (window.thisNibud) - and suddenly I got true ! On the one hand, so it should be. On the other hand, comparing with the previous test, we immediately see that the global variable and the property of the window object of the same name behave quite differently when not defined: it turns out that the property can be passed to a function without error, and with a global variable this will not work do unseen.

For the Dragonfly console, such rigor and such willingness to look for errors is still normal. The whole problem is that Opera is perfectly suited to the javascripts on the web pages with the same measure, so the Underscore.js library's _.isUndefined () function becomes, in general, useless: it won't be possible to apply it to a simple global variable , and if every time to use the prefix window. ", Then what was a fuss? - it is easier to check for uncertainty in the traditional way:

 typeof  == 'undefined' //   _.isUndefined(window.) //     ! 

To get around this problem, you have to write something like this elegant and miniature crutch:

 _.isUndef = function(name){ return _.isUndefined(window[name]); }; 

You can compare and make sure that after this, checking global variables for uncertainty acquires a short, error-free look:

 _.isUndefined() //    Opera! _.isUndef('') //       }; 

So far I have been talking about Opera, but the problem, as I understand it, is much wider than the Opera browser. For example, it is easy to show that all the same is peculiar to the Node.JS engine:

[screenshot of NodeJS]

It is clear that the analogue of the above crutch will be able to support the check of undefined global variables and in this case only a global object in it will need to write not a window , but a global , as is customary in Node.

I suspect that you can expect a similar effect when working with undefined global variables in some other applications that use the V8 engine to interpret javascripts. TheShock tells me in the comments that Google Chrome is one of these applications.

It is not difficult to make sure that you also cannot manage to call the _.isUndefined (that) code in the Web Console in Firefox. The javascript error (“ whatever is not defined ”) is caused by an attempt to load the following HTML code into Firefox 11 :

 <!doctype html> <script> isUndefined = function(i){ return i === void 0; } main = function(){ document.getElementById('b').innerHTML = isUndefined(whatever); } </script> <body id="b" onload="main();"> </body> </html> 

Everywhere, everywhere undefined global variables will have to be checked before using them as parameters of functions.

Source: https://habr.com/ru/post/140433/


All Articles