Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uses of colon in javascript

  1. Assign object literal properties var foo = { bar : 'hello'};
  2. Ternary var cats = happy ? "yes" : "no";
  3. Label a statement outer_loop: for(i=0; i<3; i++)
  4. What else?

I'm poking through a sharepoint 2010 file and I keep running into this syntax

someFunction: ;

For instance, there is a file where the following function is declared near the top:

function ULSqvN() {
    var o = new Object;
    o.ULSTeamName = "SharePoint Portal Server";
    o.ULSFileName = "SocialData.js";
    return o;
}

and then later in the file we find the following

PageUrlNormalizer = function () {
    ULSqvN: ; //<---------------- This guy here --------------------------
    try {
        this._url = _normalizedPageUrlForSocialItem
    } catch (a) {
        this._url = ""
    }
};

What is this doing?

jsFiddle with full file. This same ULSqvN: ; occurs 47 times in the file.

edit: Added full code.

PS: Consensus seems to be that the sharepoint use of colon is "not actual javascript, possibly used as a marker for some external purpose". The browser sees it as a vestigial label and so causes no errors. Thanks for all the replies, I have left the actual uses at the top so that the question contains the appropriate answers. question about same code

like image 494
Sinetheta Avatar asked Jan 20 '12 19:01

Sinetheta


People also ask

What Does a colon do in code?

Functions of the colon(:) A colon is used to represent an indented block. Another major use of the colon is slicing. In slicing, the programmer specifies the starting index and the ending index and separates them using a colon which is the general syntax of slicing.

What Does a colon do in TypeScript?

The type syntax for declaring a variable in TypeScript is to include a colon (:) after the variable name, followed by its type. Just as in JavaScript, we use the var keyword to declare a variable. Declare its type and value in one statement.

What does '$ mean in JavaScript?

$ is simply a valid JavaScript identifier. JavaScript allows upper and lower letters, numbers, and $ and _ . The $ was intended to be used for machine-generated variables (such as $0001 ). Prototype, jQuery, and most javascript libraries use the $ as the primary base object (or function).

What is question mark in JS?

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.


2 Answers

It has no purpose javascript-wise (since in javascript you would only label something that you can continue or break), it might be used as a some kind of comment or marker for some parsing script. At worst it's just dead code with no purpose whatsoever.

Edit: looks like sharepoint uses it for some diagnostic information: What does this Javascript code do?

like image 181
Esailija Avatar answered Oct 25 '22 15:10

Esailija


These are labels. Usually they are used to break out of several nested loops at once.

Example

function foo ()
{
    dance:
    for(var k = 0; k < 4; k++){
        for(var m = 0; m < 4; m++){
            if(m == 2){
                break dance;
            }
        }
    }
}

Credit: this answer.

like image 27
Sergio Tulentsev Avatar answered Oct 25 '22 14:10

Sergio Tulentsev