473,809 Members | 2,781 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.isEleme nt(object)) return;

var results = [];
for (var property in object) {
var value = Object.toJSON(o bject[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.isUnde fined(value))
results.push(pr operty.toJSON() + ': ' + value);
}

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

Thanks,
Jun 27 '08 #1
2 1053
"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(expre ssion) 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*******@gmai l.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.prototyp e 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.isEleme nt(object)) return;

var results = [];
for (var property in object) {
var value = Object.toJSON(o bject[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.isUnde fined(value))
results.push(pr operty.toJSON() + ': ' + value);
}

return '{' + results.join(', ') + '}';
},
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.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
1458
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
17811
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. Yahoo store was originally written in Lisp. c. Emacs The issues with these will probably come up, so I might as well mention them myself (which will also make this a more balanced
16
2200
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 with a class. I just don't grasp the whole concept, and how to do it. I want to make a Collectable Card Game Draft Engine...(if any of you play VS System, LOTR, Magic: The Gathering, you know what I am talking about.) It would be way to...
19
3884
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 ReuseParameterValuesOnRefresh="True" in a viewer, but it still doesn't work. Did anyone run into this problem. What's the solution? Please help. Thank you
61
3395
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 there a way to remove the ones with size predefined from the autolisting of types when I am declaring something? -- To Email Me, ROT13 My Shown Email Address
17
2549
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 website. When I click in Google on one of these pages (not index.html or home.html), I am only linked to that one html-page and not to the website itself. Does anyone know how to fix this. Is there for example a metatag? Thanks
2
1773
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
1557
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
320
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 well it already worked in 2.4. but exchanging the function with the statement yields exactly the same results Yes ;-) done.
7
1862
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 code in searches or on the MVP and tool sites I visited - though I might not understand that there is code that just needs adaptation. ALL help is appreciated. He wants a routine (that I'd put into a module) to do the following:
0
9721
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9602
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10120
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7661
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5688
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.