10

The other day, I came across some weird code:

var OrderSupplement = function() {
      oid: null;
      code: "";
      description: "";
      startdate: "";
      enddate: "";
      gender: null;
      cardowner: null;
      box: null;
      divisor: 0;
      created: null;
      createdBy: "";
};

The intention of the code was clear to me: It was the try to define a constructor function, in order to create an instance of OrderSupplement.

I am baffled by this syntax.

The buddy, who wrote the code, said it worked fine - although it obviously does not; at least it does not what it should. He instantiated a new instance of OrderSupplement and set e.g. oid to a nonnull value and retrieved it later. Of course retrieving a value before setting would have unveiled the bug.

The effect of the code above is like:

var OrderSupplementJson = function() {}

My question is:

Why is the code above accepted and does not throw an (syntax) error of any kind?

1

2 Answers 2

13

Because oid: defines a label.

It's syntactically correct but void of anything useful. It's obviously a bug.

1
  • Oh, I missed labels :D Commented Nov 7, 2014 at 8:56
4

The answer explaining that the keys are parsed as labels is correct, but I just wanted to correct the premise of the question.

This is not JSON syntax. JSON uses , to delimit values, this uses ;. JSON requires that all string keys be quoted, this does not quote them (it would be invalid within labels).

However, I may know how the author came up with this. Though useless as JavaScript, this syntax may be used to define a function returning an Object literal in CoffeeScript!

The following CoffeeScript:

OrderSupplement = ->
      oid: null;
      code: "";
      description: "";
      startdate: "";
      enddate: "";
      gender: null;
      cardowner: null;
      box: null;
      divisor: 0;
      created: null;
      createdBy: "";

Compiles to the following JavaScript:

var OrderSupplement;

OrderSupplement = function() {
  return {
    oid: null,
    code: "",
    description: "",
    startdate: "",
    enddate: "",
    gender: null,
    cardowner: null,
    box: null,
    divisor: 0,
    created: null,
    createdBy: ""
  };
};

It may also interest you to know that something quite similar also works in Firefox, through their non-standard "expression closures" lambda function syntax.

var OrderSupplement = function() ({
      oid: null,
      code: "",
      description: "",
      startdate: "",
      enddate: "",
      gender: null,
      cardowner: null,
      box: null,
      divisor: 0,
      created: null,
      createdBy: ""
});
1
  • 2
    Oh, nice. Yes, I know, that it isn't 100% JSON - but I didn't know how to describe it adequate. JSON was the first thing, that came to my mind. I asked how the author came up with this. The answer: He looked at code I've written and somehow mixed it up with Object notation - and changed all commas to semicolons to please the parser. He insisted, that I've written code like that - but when he showed me the source he saw the difference. Commented Nov 8, 2014 at 22:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.