Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Semicolon and self executing anonymous functions #516

Closed
Uxio0 opened this issue May 10, 2016 · 4 comments

Comments

@Uxio0
Copy link

commented May 10, 2016

Standard format is breaking my code removing the semicolons.

You just have to take Facebook SDK Api Example:

function load() {
    window.fbAsyncInit = function() {
        FB.init({
            appId      : 'xxxxxxxxxxxxxxxx',
            cookie     : true,  // enable cookies to allow the server to access
            // the session
            xfbml      : true,
            version    : 'v2.6'
        })
        checkLoginState()
    };

    (function(d, s, id) {
        var js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) return;
        js = d.createElement(s); js.id = id;
        js.src = "//connect.facebook.net/en_US/sdk.js";
        fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
}

It works. Notice that if you remove the semicolon on }; you get an error FB is not defined.

When it's formatted to standard-format, it's not working anymore (FB not defined):

function load () {
  window.fbAsyncInit = function () {
    FB.init({
      appId: 'xxxxxxxxxxxxxxxx',
      cookie: true, // enable cookies to allow the server to access
      // the session
      xfbml: true,
      version: 'v2.6'
    })
    checkLoginState()
  }(function (d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0]
    if (d.getElementById(id)) return
    js = d.createElement(s); js.id = id
    js.src = '//connect.facebook.net/en_US/sdk.js'
    fjs.parentNode.insertBefore(js, fjs)
  }(document, 'script', 'facebook-jssdk'))
}

Maybe I'm doing something wrong.

@yoshuawuyts

This comment has been minimized.

Copy link
Contributor

commented May 10, 2016

You probably want to do:

;(function (d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0]
    if (d.getElementById(id)) return
    js = d.createElement(s); js.id = id
    js.src = '//connect.facebook.net/en_US/sdk.js'
    fjs.parentNode.insertBefore(js, fjs)
  }(document, 'script', 'facebook-jssdk'))

standard-format is not perfect unfortunately, but I hope this resolves your issue at least for now. Cheers!

@Uxio0

This comment has been minimized.

Copy link
Author

commented May 10, 2016

Oh, thanks for your fast reply! It's working like you say. Could you give me a brief explanation on why it's working?

Cheers!

@dcousens

This comment has been minimized.

Copy link
Member

commented May 10, 2016

@Uxio0 my 2 second explanation:

var foo = function foo (z) { return z }
(9)

console.log(foo)
// => 9

Compared to

var foo = function foo (z) { return z }
;(9)

console.log(foo)
// => [Function: foo]
@Uxio0

This comment has been minimized.

Copy link
Author

commented May 10, 2016

Awesome @dcousens and @yoshuawuyts, thank you very much!

@dcousens dcousens added the question label May 10, 2016

@dcousens dcousens closed this May 10, 2016

@lock lock bot locked as resolved and limited conversation to collaborators May 10, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
3 participants
You can’t perform that action at this time.