The question is purely out of curiosity: I often meet such a construction in the code - var ie = document.all? 1: 0; If it is known in advance that the variable ie will only be used to check for true / false, is it not easier to declare it var ie = document.all; ? What is the difference?
2 answers
In case document.all is defined, document.all is written to the ie variable, and not true. The ternary operator is done in order not to drag an object into the variable ie.
- 2Yes, perhaps, this is closer to the truth: the set of type conversions is replaced by one, at the very beginning. By the way, in ie not the object is dragged, but the reference to the object. <script> alert (document.all ['b']); var a = document.all; a ['b'] = 1; alert (document.all ['b']); </ script> - check in IE - ling
- Of course the link in js objects are always passed by reference. As far as I know, there is no built-in "cloning" of objects in js, only custom implementations. - charlie
|
Better then
var ie = !! document.all;
And even better not to use checks on a specific browser at all, and all this has nothing to do with IE.
- I mean: do we need all these type conversions and unnecessary actions when writing comprehensible code? Moreover, the best test for IE that I have encountered is var ie =! - [1,]; from Aleko. A check on the browser is sometimes needed, for example, for dynamically loading styles. - ling
- oneIt is better not to do a browser check using hacks :) Why do you need a browser check at all? - Alex Silaev
- I would not use a cast in expressions like if (navigator.geolocation) and use it when saving to a variable. And instead of dynamically loading styles for IE, it’s better to connect them with conditional comments. - Artyom Sapegin
- Purely hypothetically, verification is useful when displaying information on another site by inserting one script tag (on the same principle as the counters). There is no specific task yet, but every shit happens. But something we have moved away from the topic. So is there a difference between var ie = document.all? 1: 0; and var ie = document.all; ? - ling
|