Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code inside javascript function separated with commas instead of semicolons [duplicate]

I am trying to understand the behaviour of a jQuery plugin but I don't understand the way it is coded. I did my best to no avail. Could you please give me a link explaining this way of coding?

    (function($) {
        $.widget("ui.chatbox", {
        ....
         _create: function() {
                    var self = this,
                    options = self.options,
                    title = options.title || "No Title",

                    uiChatbox = (self.uiChatbox = $('<div></div>'))
                    .appendTo(document.body)
                           }
        .... 
     }(jQuery));

PS: The whole code is a widget created by the jQuery UI Widget Factory. You can see the whole code here.

In the official documentation example, the _create content is separated by semicolon, what is usual (for me). Help is appreciated.

like image 930
Adib Aroui Avatar asked Aug 25 '26 09:08

Adib Aroui


1 Answers

In javascript, when declaring new variables with the var keyword, you can separate each declaration by commas. Which allows you to only use the var keyword once. Its a sort of short-hand.

For example:

var self = this,
    options = self.options,
    title = options.title || "No Title",
    uiChatbox = (self.uiChatbox = $('<div></div>')).appendTo(document.body)

Is the same as:

var self = this;
var options = self.options;
var title = options.title || "No Title";
var uiChatbox = (self.uiChatbox = $('<div></div>')).appendTo(document.body);
like image 56
Nicholas Thomson Avatar answered Aug 26 '26 21:08

Nicholas Thomson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!