admin管理员组文章数量:1332865
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
2 Answers
Reset to default 13Because 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: ""
});
本文标签: Why does JavaScript parser accept JSON as a function bodyStack Overflow
版权声明:本文标题:Why does JavaScript parser accept JSON as a function body? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742292288a2448032.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论