2

I was just looking through some code from a few days ago and I noticed this snippet:

function getTextWidth(text, font) {

    http: //stackoverflow.com/a/21015393/1413853

        var canvas = getTextWidth.canvas || (getTextWidth.canvas = document
            .createElement("canvas"));
    var context = canvas.getContext("2d");
    context.font = font;
    var metrics = context.measureText(text);
    return metrics.width;
}

Notice the line:

http: //stackoverflow.com/a/21015393/1413853

Which should actually be commented:

// http://stackoverflow.com/a/21015393/1413853

I would have thought that this was invalid JS syntax, yet it compiles and runs correctly in Chrome.

Is this actually meant to be used for something, or is Chrome just being lenient?

I've noticed that this works too:

function getTextWidth(text, font) {

    a: 
    b: 
    c: 

    return 1
}
Eddie Fletcher
  • 2,823
  • 2
  • 21
  • 23
  • `http:` is considered a label which can be used in a break/continue statement... then // makes the rest a comment ... that line goes nowhere does nothing basically – Jaromanda X Jan 17 '17 at 00:36

2 Answers2

3

From the documentation at MDN label

The labeled statement can be used with break or continue statements. It is prefixing a statement with an identifier which you can refer to.

so http: is a label - the rest is a comment - perfectly valid javascript syntax

Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
2

That is a label (MDN page).

Javascript: giving you all the footguns you could ever want!

GregL
  • 37,147
  • 8
  • 62
  • 67