Asked  6 Months ago    Answers:  5   Viewed   24 times

In several JavaScript libraries I saw this notation at the very beginning:

/**
 * Library XYZ
 */
;(function () {
  // ... and so on

While I'm perfectly comfortable with the "immediately executed function" syntax

(function(){...})()

I was wondering what the leading semicolon is for. All I could come up with is that it is an insurance. That is, if the library is embedded in other, buggy code, it serves as an "the last statement ends here at the latest" kind of speed bump.

Has it got any other functionality?

 Answers

73

It allows you to safely concatenate several JavaScript files into one, to serve it quicker as one HTTP request.

Tuesday, June 1, 2021
 
THEK
answered 6 Months ago
34
a[0] |= b

is basically

a[0] = a[0] | b

"|" is an or bitwise operator

Update When a[0] is assigned 0, a[0] in binary is 0000. In the loop,

  1. b = 0

    a[0] = 0 (base 10) = 0000 (base 2)
    b    = 0 (base 10) = 0000 (base 2)
                       ---------------
    a[0] | b           = 0000 (base 2) = 0 (base 10)
    
  2. b = 1

    a[0] = 0 (base 10) = 0000 (base 2)
    b    = 1 (base 10) = 0001 (base 2)
                       ---------------
    a[0] | b           = 0001 (base 2) = 1 (base 10)
    
  3. b = 2

    a[0] = 1 (base 10) = 0001 (base 2)
    b    = 2 (base 10) = 0010 (base 2)
                       ---------------
    a[0] | b           = 0011 (base 2) = 3 (base 10)
    
  4. b = 3

    a[0] = 3 (base 10) = 0011 (base 2)
    b    = 3 (base 10) = 0011 (base 2)
                       ---------------
    a[0] | b           = 0011 (base 2) = 3 (base 10)
    
  5. b = 4

    a[0] = 3 (base 10) = 0011 (base 2)
    b    = 4 (base 10) = 0100 (base 2)
                       ---------------
    a[0] | b           = 0111 (base 2) = 7 (base 10)
    
  6. b = 5

    a[0] = 7 (base 10) = 0111 (base 2)
    b    = 5 (base 10) = 0101 (base 2)
                       ---------------
    a[0] | b           = 0111 (base 2) = 7 (base 10)
    
  7. b = 6

    a[0] = 7 (base 10) = 0111 (base 2)
    b    = 6 (base 10) = 0110 (base 2)
                       ---------------
    a[0] | b           = 0111 (base 2) = 7 (base 10)
    
  8. b = 7

    a[0] = 7 (base 10) = 0111 (base 2)
    b    = 7 (base 10) = 0111 (base 2)
                       ---------------
    a[0] | b           = 0111 (base 2) = 7 (base 10)
    
  9. b = 8

    a[0] = 7 (base 10) = 0111 (base 2)
    b    = 8 (base 10) = 1000 (base 2)
                       ---------------
    a[0] | b           = 1111 (base 2) = 15 (base 10)
    
  10. b = 9

    a[0] = 15 (base 10) = 1111 (base 2)
    b    =  9 (base 10) = 1001 (base 2)
                        ---------------
    a[0] | b            = 1111 (base 2) = 15 (base 10)
    

At the end of the loop the value of a[0] is 15

Tuesday, July 13, 2021
 
Elxx
answered 5 Months ago
51
{

at the start of a statement signals a ‘statement block’ (see ECMA-262-3 section 12.1), which contains a list of statements.

}

immediately ends the statement block with no statements in it. That's fine. But now the parser is looking for the next statement:

==false

Huh? That's not a statement; syntax error.

What are statement blocks for? Well, you are writing a statement block every time you say:

if (something) {
    ...
}

JavaScript defines these flow-control statements as:

if "(" <expression> ")" <statement> [else <statement>]

ie. in the single statement form with no braces. It then allows you to use a statement-block anywhere you can use a single statement, which means you can have if-braces-many-statements. But it also means you can have a statement-block on its own with no associated flow-control statement.

This serves absolutely no practical purpose! You might be tempted to think it gave you information-hiding, but no:

var a= 1;
{
    var a= 2;
}
alert(a);

...results in 2, because statement blocks don't in themselves create a new scope.

JavaScript defines flow control and statement blocks in this manner because C (and other languages derived from it) did. Those languages didn't make {} serve double-duty as an Object literal expression though, so they didn't have the ambiguity that makes this another JS misfeature.

Even this wannabe-literal:

{
   a: 1
}

is a valid statement block, because ‘:’ is used to denote a label in a statement. (and 1 is a useless expression-statement, with the semicolon omitted.) Labels are another feature inherited from C that are rarely used in JavaScript. They're not totally pointless like the blocks, but they're seldom needed and often considered in poor taste.

(A literal with two properties will cause a syntax error, as object literals use comma separators, but labelled statements must be separated by semicolons.)

This is not the only place where JavaScript's loose syntax can trip you up by making some other statement of something you think is an expression.

Thursday, July 29, 2021
 
commonpike
answered 4 Months ago
98

return only makes sense inside a function. There is no function in your code.

Also, your code is worthy if the Department of Redundancy Department. Assuming you move it to a proper function, this would be better:

return confirm(".json_encode($message).");

EDIT much much later: Changed code to use json_encode to ensure the message contents don't break just because of an apostrophe in the message.

Monday, August 2, 2021
 
LOKESH
answered 4 Months ago
47

The pipeline operator (|>) is a non-standard experimental operator (currently, works only in Firefox, by enabling it explicitly), which passes its first operand as an argument to its second operand (which should be a function).

Its goal is to make function chaining more readable.

Translated to this case, it will look like this:

const max = (_ => Math.max(..._))(
  Object.values({a: 1, b: 2, c: 3})
)

console.log(max) //3
  • First, pass {a: 1, b: 2, c: 3} to Object.values
  • Then, pass the result to the anonymous function (_ => Math.max(..._))
  • Finally, assign the output to variable max
Sunday, August 22, 2021
 
Webroots
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share