Why is the Twitter script tag trying to evaluate true? Does it matter?

Possible duplicate:
What does an exclamation mark do before a function?

I maintain an existing site with a tweet button that appears to come from this page

A colleague had a big question: why does this script tag override the value of this self-starting function ( undefined without return)? (quite easy to read, the original is in the link)

 <script> !function(d,s,id) { //^--- that var js,fjs=d.getElementsByTagName(s)[0]; if(!d.getElementById(id)) { js=d.createElement(s); js.id=id; js.src="https://platform.twitter.com/widgets.js"; fjs.parentNode.insertBefore(js,fjs); } }(document,"script","twitter-wjs"); </script> 

In my eyes, other than JS-pro, it seems like it flips the boolean value to undefined , thereby allowing the <script> evaluate to true. For what it's worth, for me it's a completely different idea (the <script> has the meaning of an expression).

My expectation is that there is a specific browser for which the logical value of the <script> tag matters (yes, I feel crazy even typing this).

So, the main question is: Does <script> meaningful "expression" for any browsers you know about? and What is the implicit meaning of true vs. false in this case?

+4
source share
2 answers

An explanation of what is happening and why ...

In Javascript, you have function statements and function expressions . Both are similar, but not accurate.

Function operator

 function myTest() {return true;} 

Function Expressions

 var f = function() {return true;} alert(f()); // alert(function() { return true}); // Return value is used // and even (function(x, y) { var z, x; // private variables, hidden from the outside scope var r = x + y; // Return value is ignored. }(1, 2)); // The ! can be used to start a function expression too. // This is totally legal Javascript, but it isn't in the normal // "vernacular" of the langage !function(x, y) { var z, z1; // Private vars // Do something with side effects // Return value is ignored. }(x, y); 

Updated due to comments

 //The following code works identical: var v1 = (function(a, b) {return a+b}(1,2)); var v2 = (function(a, b) {return a+b})(1,2); var v3 = function(a, b) {return a+b}(1,2) 

I prefer the first form because it allows me to use the block matching tools in my editor, and the f1 form is sometimes preferred by the useful JSLint and JSHint programs .

All create a function expression and then call it immediately. In this case, the partners do not need the Javascript compiler, but they provide very useful advice to the reader that this is not a normal purpose.

On the other hand, you have to tell the JS engine something that you have a function expression instead of a function operator. The = sign above works, but when there is no destination, you need to start with some kind of operator, be it (+!

 !function(x, y) {alert(x, y)}(1, 2); 

The lead statement makes this expression. () in the above examples also leads to its expression.

+4
source

This code creates a new script tag and adds it before the first element of the script type if there is no script element with twitter-wjs .

So how ! is a NOT operator in Javascript, it evaluates to true when its operand can be converted to false (in this case, when there is no element with id twitter-wjs , document.getElementById("twitter-wjs") returns undefined , and this is in Javascript is a false value, so the result, combined with the ! operator ! will be true and the code block inside if will execute. Any object in Javascript will be true when checking for its boolean value (try to do it !![Object] , and you will prove it), so the block if will not be executed when any element is returned document.getElementById() .

UPDATE:
As for your comments, I see that @JeremyJStarcher's answer is ok, but I am adding something:
Your code has a function expression . for function expressions identifier is not required. But when executed, the Javascript interpreter may be confused and think of it as a function declaration . In function declarations identifier is not optional, so the interpreter will throw a syntax error. To avoid this, the operator is used here ! , therefore, it will be considered as a function expression .

0
source

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


All Articles