From: M A Hossain Tonu on
JSON stands for JavaScript Object Notation. The current trend among
many JavaScript developers seems to be to write code JSON style, i.e.
to collect a number of functions/methods into a singleton object
written in object notation.

What! did i heard Singleton Object? Whats dat ?

Well Wiki says: The singleton pattern is a design pattern that is used
to restrict instantiation of a class to one object

Actually JSON objects are singleton objects by design, not by choice,
since they have no constructor. I really like JSON

So using the JSON you can define a class, while at the same time
creating an instance (object) of that class, means that you can have
only one single instance of this class at any time, you cannot
instantiate more objects of the same class.

I have written here more:

http://mahtonu.wordpress.com/2010/04/13/json-style-javascript-object-declaration/
From: RobG on
On Jun 3, 5:57 pm, M A Hossain Tonu <maht...(a)gmail.com> wrote:
> JSON stands for JavaScript Object Notation. The current trend among
> many JavaScript developers seems to be to write code JSON style, i.e.
> to collect a number of functions/methods into a singleton object
> written in object notation.
>
> What! did i heard Singleton Object? Whats dat ?
>
> Well Wiki says: The singleton pattern is a design pattern that is used
> to restrict instantiation of a class to one object

JSON is a means of describing objects using javascript object literal
syntax, nothing more. It is not intended to be a singleton pattern,
nor does it restrict a "class" to one object only. Consider:

var jsonString = "{foo:'foo', bar:'bar'}";

var obj1 = eval('(' + jsonString + ')');
var obj2 = eval('(' + jsonString + ')');

Would you say obj1 and obj2 are two instances of the same "class"?
Javascript has no classes, using classic class-baesd OO terminology to
describe a language that does not have classes only confuses.


> Actually JSON objects are singleton objects by design, not by choice,
> since they have no constructor. I really like JSON

What is a "JSON object"? If it's an object created by eval'ing a JSON
string, then not only is it not possible to tell afterward how it was
created (i.e. to tell it's a "JSON object"), but also it is simple to
create a second "instance" as shown above. How is this a "singleton"?


> So using the JSON you can define a class, while at the same time
> creating an instance (object) of that class, means that you can have
> only one single instance of this class at any time, you cannot
> instantiate more objects of the same class.

Not correct, as shown above.

As I understand it, the primary motivation for using an object literal
as you describe is as a simple method to provide a "namespace" for
what would otherwise be global functions and variables. An alternative
is:

var obj1 = {};
obj1.foo = 'foo';
obj1.bar = 'bar';

The result is indistinguishable from the JSON version above.


--
Rob
From: Ry Nohryb on
On Jun 3, 9:57 am, M A Hossain Tonu <maht...(a)gmail.com> wrote:
> JSON stands for JavaScript Object Notation. The current trend among
> many JavaScript developers seems to be to write code JSON style, i.e.
> to collect a number of functions/methods into a singleton object
> written in object notation. (...)

One thing is for sure: if it contains a function / method, it's *not*
a JSON text... :-)

See: http://json.org/
--
Jorge.
From: Richard Cornford on
On Jun 3, 1:45 pm, Ry Nohryb wrote:
> On Jun 3, 9:57 am, M A Hossain Tonu wrote:
>
>> JSON stands for JavaScript Object Notation. The current trend
>> among many JavaScript developers seems to be to write code
>> JSON style, i.e. to collect a number of functions/methods into
>> a singleton object written in object notation. (...)
>
> One thing is for sure: if it contains a function / method,
> it's *not* a JSON text... :-)
>
> See:http://json.org/

"Contains" is a bit ambiguous. In principle there is no reason for
javascript source code not to be string data within a JSON text, at
which point the odds are good that the JSON will 'contain' a function
(in one sense).

If a piece of text (sequence of characters) is such that its
processing as javascript source text by an (ECMAScript conforming)
javascript engine would result in the creation of a javascript
function object then that text is not JSON (does not satisfy the JSON
syntax rules).

There is a huge confusion between javascript object literal notation
and the restricted sub-set of its syntax that defines JSON. Any talk
of "a JSON object" is probably a good indicator that this confusion is
involved.

Richard.
From: Ry Nohryb on
On Jun 3, 3:04 pm, Richard Cornford <Rich...(a)litotes.demon.co.uk>
wrote:
> (...)
> There is a huge confusion between javascript object literal notation
> and the restricted sub-set of its syntax that defines JSON. Any talk
> of "a JSON object" is probably a good indicator that this confusion is
> involved.

<FAQENTRY> JSON !== JSOL </FAQENTRY>

:-)
--
Jorge.