what is the syntax in the latest json.js

S

Sun Liwen

Hi, all:

I met something wierd when I reading the latest json.js.
look at the code segment.
especially notice the "START HERE" and "END HERE" place...

(function () { // <-------------------------- START HERE
var m = {
'\b': '\\b',
'\t': '\\t',
'\n': '\\n',
'\f': '\\f',
'\r': '\\r',
'"' : '\\"',
'\\': '\\\\'
},
s = {
array: function (x) {
var a = ['['], b, f, i, l = x.length, v;
for (i = 0; i < l; i += 1) {
v = x;
f = s[typeof v];
if (f) {
v = f(v);
if (typeof v == 'string') {
if (b) {
a[a.length] = ',';
}
a[a.length] = v;
b = true;
}
}
}
a[a.length] = ']';
return a.join('');
},
'boolean': function (x) {
return String(x);
},
'null': function (x) {
return "null";
},
number: function (x) {
return isFinite(x) ? String(x) : 'null';
},
object: function (x) {
if (x) {
if (x instanceof Array) {
return s.array(x);
}
var a = ['{'], b, f, i, v;
for (i in x) {
v = x;
f = s[typeof v];
if (f) {
v = f(v);
if (typeof v == 'string') {
if (b) {
a[a.length] = ',';
}
a.push(s.string(i), ':', v);
b = true;
}
}
}
a[a.length] = '}';
return a.join('');
}
return 'null';
},
string: function (x) {
if (/["\\\x00-\x1f]/.test(x)) {
x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) {
var c = m;
if (c) {
return c;
}
c = b.charCodeAt();
return '\\u00' +
Math.floor(c / 16).toString(16) +
(c % 16).toString(16);
});
}
return '"' + x + '"';
}
};

Object.prototype.toJSONString = function () {
return s.object(this);
};

Array.prototype.toJSONString = function () {
return s.array(this);
};
})(); // <-------------------------- END HERE

Why there is a bracket outside the anonymous function?

I have did a quick browse of ECMA-262.pdf, but did not find what I want
to know.
A search for bracket will not get any useful result.

Thank you all!
 
R

Richard Cornford

Sun Liwen wrote:
(function () { // <-------------------------- START HERE
})(); // <-------------------------- END HERE

Why there is a bracket outside the anonymous function?

The whole is an ExpressionStatement. ExpressionStatements are explicitly
forbidden from starting with - function -, else they could not be
distinguished from FunctionDeclarations. The first set of parentheses
around the anonymous function expression ensure that it cannot be
interpreted as a Function Declaration, the final pair of parentheses are
call operators and call the value of the Expression to their left (the
parenthesised anonymous function expression, for which the value is a
reference to a function object).
I have did a quick browse of ECMA-262.pdf, but did not
find what I want to know.
A search for bracket will not get any useful result.

A search for function would have found the restriction on the
ExpressionStatement production.

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top