None <jd*******@gmail.comwrites:
toJSON: function(object) {
"toJSON" is a function somewhere that that takes a value as argument
and return a string value containing a JSON literal representing the
original value, if possible.
var type = typeof object;
switch (type) {
case 'undefined':
case 'function':
case 'unknown': return;
case 'boolean': return object.toString();
}
if (object === null) return 'null';
At this point we know that the value is not a function or boolean,
and is not undefined or "unknown" (a non-standard type returned by
some browsers' host objects).
if (object.toJSON) return object.toJSON();
--------------------------------------if toJSON is a function name,
it is a function that extends the Object
If the original value, converted to an object if necessary (e.g., a
number or a string) has a property called "toJSON", then call the
value of that property as a method and return the result.
This will catch, e.g., if String.prototype or Array.prototype has had
a "toJSON" method added, or if a custom object has its own method.
if object.toJSON is true and the return object.toJSON(), what is the
mean?
If the property object.toJSON's value, converted to a boolean, is true,
then return the result of calling object.toJSON().
Converting a function to a boolean gives "true". Converting the value
of an undefined property gives "false".
It is like a loop.
It's a recursive call. Remember, the "toJSON" method on the object is
not the same as the current toJSON function (or at least it shouldn't
be, since the current one expects an argument).
if (Object.isElement(object)) return;
var results = [];
for (var property in object) {
var value = Object.toJSON(object[property]);
---------------------------------I think the value should be the value
of the property.But from the code above, I can not understand why
value can be the value of the property
Not understood.
I'm assuming that Object.toJSON is the function we are currently looking
at.
This loop runs through the enumerable properties of the original object
and converts them to a JSON literal string, one by one, using this method.
Then it collects them in an array, and creates a JSON object literal
containing those properties.
if (!Object.isUndefined(value))
results.push(property.toJSON() + ': ' + value);
}
return '{' + results.join(', ') + '}';
},
/L
--
Lasse Reichstein Nielsen -
lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'