473,399 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Don't understand these code.

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
2 1040
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: L'astemio | last post by:
hi all i must send a mail from a php form. ok. i have pear installed. ok. this is my code: <? include("Mail.php"); $recipients = "info@domain.com"; $headers = "info@domain.com";
303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
16
by: Jace Benson | last post by:
Ok I have read alot of things on zend.com, php.net and other sites went to the wikibooks to try to understand how to use a class. I have this project I want to do that I am sure would work great...
19
by: LP | last post by:
I am using (trying to) CR version XI, cascading parameters feature works it asks user to enter params. But if page is resubmitted. It prompts for params again. I did set...
61
by: John Baker | last post by:
When declaring an integer, you can specify the size by using int16, int32, or int64, with plain integer being int32. Is integer the accepted default in the programming community? If so, is...
17
by: =?Utf-8?B?Y2F0aGFyaW51cyB2YW4gZGVyIHdlcmY=?= | last post by:
Hello, I have build a website with approximately 30 html-pages. When I search this website in Google, I see the index.html or home.html on this website, but also other html-pages on this...
2
by: xianwei | last post by:
First, typedef struct pair { Node *parent; Node *child; } Pair; static Pair SeekItem(cosnt Item *pI, const Tree *pTree) { Pair look;
3
by: Ben Thomas | last post by:
Hello, I have the following code which I don't understand why it works : #include <iostream> using namespace std; void DontWork (unsigned int& i) { cout << i << endl; }
0
by: Stef Mientki | last post by:
Terry Reedy wrote: sorry, don't know how this happened, as I always copy/paste ? AFAIK locals() == sys._getframe(0).f_locals AFAIK, again one level up weird, I use it in 2.5 and if I remember...
7
by: sara | last post by:
I have a friend doing some pro-bono work for a non-profit that does job training for distressed kids under DCSS care. He asked me for code to do the following (he's using A2003). I can't find...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.