📜 ⬆️ ⬇️

Javascript: Good code style is usually

image Banal teaching for novice programmers :)

Mom told me: “Son, declare variables explicitly, even if it seems to be not necessary!”, But I was all lazy.

Simple code in javascript:
<script>
msg='';
alert(msg);
</script>


Everything is simple and clear and works brilliantly under FireFox. As a cultural programmer, I check the page under IE and are surprised to get the error 'Object doesn't support this property or method' on the line where the assignment is. Those. variable can not assign a value! Some kind of nonsense at first glance, especially for those who for the first time catch a cutting of such a rake with their foreheads.
')
After a half-hour search (the first 20 minutes were spent on lifting the fallen jaw from the floor), it turned out that everything was just indecent. Many readers probably already guessed, especially if they themselves caught such a rake.
The webmaster who has installed the template has added an innocent block to the page
<div id='msg'>- </div>
And Internet Explorer loves to simplify life for programmers, while doing everything just harder.
And from such unearthly love, IE created a variable msg that corresponded to this div. And when we try to assign a value to this variable, we get a natural bounce.
The solution is trivial:

<script>
var msg='';
alert(msg);
</script>


After that, the browser with the programmer has full understanding and love :)
Good luck!

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


All Articles