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?
JSON.parse() A common use of JSON is to exchange data to/from a web server. When receiving data from a web server, the data is always a string. Parse the data with JSON.parse() , and the data becomes a JavaScript object.
JSON is a JavaScript-based object/value encoding format that looks very close to raw JavaScript and can be very easily parsed by JavaScript code because JavaScript can effectively evaluate a JSON string and re-materialize an object from it.
JSON. parse() is a crucial method for converting JSON data in string form into Javascript objects. It is possible to convert simple or complex objects, but you should never convert calculations or code, like for loops.
JSON is faster because it is designed specifically for data interchange. JSON encoding is terse, which requires less bytes for transit. JSON parsers are less complex, which requires less processing time and memory overhead. XML is slower, because it is designed for a lot more than just data interchange.
Because oid:
defines a label.
It's syntactically correct but void of anything useful. It's obviously a bug.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With