By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,940 Members | 1,300 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,940 IT Pros & Developers. It's quick & easy.

Don't understand these code.

P: n/a
Hi All

These code are part of Prototype. There are something that I don't
understand, would you please explain it for me?

.......
toJSON: function(object) {
var type = typeof object;
switch (type) {
case 'undefined':
case 'function':
case 'unknown': return;
case 'boolean': return object.toString();
}

if (object === null) return 'null';
if (object.toJSON) return object.toJSON();
--------------------------------------if toJSON is a function name,
it is a function that extends the Object
if object.toJSON is true and the return object.toJSON(), what is the
mean? It is like a loop.
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
if (!Object.isUndefined(value))
results.push(property.toJSON() + ': ' + value);
}

return '{' + results.join(', ') + '}';
},

Thanks,
Jun 27 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"None" wrote
......
toJSON: function(object) {
var type = typeof object;
switch (type) {
case 'undefined':
case 'function':
case 'unknown': return;
case 'boolean': return object.toString();
}

if (object === null) return 'null';
if (object.toJSON) return object.toJSON();
--------------------------------------if toJSON is a function name,
it is a function that extends the Object
if object.toJSON is true and the return object.toJSON(), what is the
mean?
object.toJSON will be the result of a function call. If the argument to that
function is of type boolean, the result will be a string, otherwise the
result will be undefined (return without a value).

The if statement checks if the value of toJSON is true (i.e. if
toBoolean(expression) is true. If it is undefined, that will be boolean
false and nothing happens. In the other case the toJSON value (a string)
will be returned.

Tom
Jun 27 '08 #2

P: n/a
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.'
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.