Amin Mousavi
Amin Mousavi

Reputation: 668

Bootstrap JS functions definitions

In bootstrap JS (I checked version 3.2.0) some function definitions have a plus sign at first and 'use strict'; in the following:

+function ($) {
  'use strict';
   ...
}

'Use strictly'; as I guess is AMD definition syntax maybe. Can someone explain about this syntax and meaning and where is better to use this pattern? Thanks.

UPDATE: It seems the + sign (Or any arbitrary unary operator) uses to turn the function declaration into an expression. Check here for more info: https://stackoverflow.com/a/11897575/332420

Upvotes: 2

Views: 202

Answers (1)

LJ Wadowski
LJ Wadowski

Reputation: 6720

"Strict mode makes it easier to write "secure" JavaScript.

Strict mode changes previously accepted "bad syntax" into real errors.

As an example, in normal JavaScript, mistyping a variable name creates a new global variable. In strict mode, this will throw an error, making it impossible to accidentally create a global variable.

In normal JavaScript, a developer will not receive any error feedback assigning values to non-writable properties.

In strict mode, any assignment to a non-writable property, a getter-only property, a non-existing property, a non-existing variable, or a non-existing object, will throw an error."

The + sign is forcing the functions to run automatically, e.g. google is using ! instead

MORE

Upvotes: 1

Related Questions