Why does JavaScript parser accept JSON as a function body? - Stack Overflow

The other day, I came across some weird code:var OrderSupplement = function() {oid: null;code: "&q

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?

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?

Share edited Nov 9, 2014 at 8:26 Boann 50.1k16 gold badges124 silver badges152 bronze badges asked Nov 7, 2014 at 8:52 Thomas JunkThomas Junk 5,6762 gold badges33 silver badges46 bronze badges 1
  • 3 See here developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – elclanrs Commented Nov 7, 2014 at 8:52
Add a ment  | 

2 Answers 2

Reset to default 13

Because oid: defines a label.

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

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: ""
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742292289a4416424.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信