Don't understand these code.

N

None

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,
 
T

Tom de Neef

......
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
 
L

Lasse Reichstein Nielsen

None said:
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
 

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,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top