2

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

3
  • That's probably an IIFE, and you missed the parens that call the function +function(){}(). use strict has nothing to do with AMD, see here developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Oct 22, 2014 at 8:07
  • Definition is exactly this "+function($){...}(jQuery);" nothing is missing. You can check it yourself here: maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.js Commented Oct 22, 2014 at 8:33
  • Right, you didn't put the parenthesis in the question, which makes +function($){} non sensical. The plus +, or any other unary operators will do +function(){}(), ~function(){}(), !function(){}(), ... Commented Oct 22, 2014 at 8:52

1 Answer 1

1

"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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. What is the meaning of that + sign just before the function keyword. Is it alternate syntax for closure syntax?
Its forcing function to run automatically, e.g. google is using ! instead +

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.