Javascript ‘colon’ for labeling anonymous functions?

You are missing some code there, but I assume its part of an object declaration like this:

var obj = {
  queryString: function() {
    //some code
  }
};
obj.queryString();

It assigns a function as a property of an object literal. It would be equivalent to this:

var obj = {};
obj.queryString = function() { ... };
obj.queryString();

In general, the object literal syntax looks like this:

{ key: value, otherKey: otherValue };

So the reason this didn’t work in the console is that it was not enclosed in {} characters, denoting an object literal. And this syntax is valid ONLY in an object literal.

Leave a Comment