From: Justin Johnson on
* Duplicating the comment I made on the blog for the benefit of the
discussion here

Two things.

1) You are using the wrong terminology. There is a confusion between
object literal notation and JSON. What you are using and describing is
*object literal notation.* JSON is intended as a means of data
transport (either between applications and/or across a network), is
always in string form, cannot define functions, and is a subset of
object literal notation.

2) While an object created in this fashion might *act* like a
singleton, it is not really a singleton. There is nothing to stop
someone from cloning the object, resulting in more than one
representation/instance of the same data.

var clone = function(o) {
var no = {};
for ( var i in o ) {
no[i] = o[i];
}
return no;
}

var a = {a: 1, b: 2},
b = clone(a),
c = a;

a.a = 3;
b.b = 4;
console.log(a, b, c);

The only way to have a true singleton in JavaScript is to use all
private members so that the data cannot be cloned from one object to
another.

var a = (function() {
var _a = 1, _b = 2;
return {
getA: function() { return _a; },
setA: function(a) { _a = a; },
getB: function() { return _b; },
setB: function(b) { _b = b; }
};
})();

var b = clone(a);

a.setA(3);
b.setB(4);
console.log(a.getA(), a.getB(), b.getA(), b.getB());

However, this might be splitting hairs. For most applications, the
object literal approach to singletons is probably sufficient.
From: M A Hossain Tonu on
@Rob, @Jorge, @Richard, @Justin:
Thank you very much for your valuable discussion, i will be updating
my post shortly.

Thnx again for the help, cheers :D
From: M A Hossain Tonu on
Hi Rob,

Now a days use of JSON doesn't limit within only in data-
interchanging.
Due to it is based on subset of JavaScript programming language itself
it starts confusion cause JavaScript itself confusing regarding OO.

Again if we use JSON format to describe an object, at the same time we
are instantiating the object.

Consider your example:

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

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

Here you have used JSON string, i am not talking about JSON string
rather talking about Object Notation. well if u consider like this:

var jsonObj = {foo:'foo', bar:'bar'};//description and instantiation
at a time here

var obj1 = jsonObj ;
var obj2 = clone(jsonObj);

Then the only object instantiated here is jsonObj not the obj1, obj2,
they are the simple copy or clone of that object though it can't be
tell afterward how it was
created(in this point you are right).

Note that there are no classes in JavaScript.
Functions can be used to somewhat simulate classes, but in general
JavaScript is a class-less language.

Everything is an object. And when it comes to inheritance, objects
inherit from objects, not classes from classes as in the "class"-ical
languages.
This scheme is called Prototype-based programming where classes are
not present rather class behavior reused.

This model can also be known as class-less, prototype-oriented or
instance-based programming. And has been grown increasingly popular
and adopted in JavaScript.


Any suggestion will be highly appreciable.

Updating my post too.

--
Tonu


On Jun 3, 2:45 pm, RobG <rg...(a)iinet.net.au> wrote:
> 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: M A Hossain Tonu on
On Jun 5, 3:03 am, Justin Johnson <booleang...(a)gmail.com> wrote:
> * Duplicating the comment I made on the blog for the benefit of the
> discussion here
>
> Two things.
>
> 1) You are using the wrong terminology. There is a confusion between
> object literal notation and JSON. What you are using and describing is
> *object literal notation.* JSON is intended as a means of data
> transport (either between applications and/or across a network), is
> always in string form, cannot define functions, and is a subset of
> object literal notation.
>
> 2) While an object created in this fashion might *act* like a
> singleton, it is not really a singleton. There is nothing to stop
> someone from cloning the object, resulting in more than one
> representation/instance of the same data.
>
> var clone = function(o) {
>     var no = {};
>     for ( var i in o ) {
>         no[i] = o[i];
>     }
>     return no;
>
> }
>
> var a = {a: 1, b: 2},
>     b = clone(a),
>     c = a;
>
> a.a = 3;
> b.b = 4;
> console.log(a, b, c);
>


a.a = 3;
b.a = 4;
console.log(a.a, b.a, c.a);

outputs 4 4 4 they are using same memory...
cause they are copy not an unique instance....so where is the
singelton pattern violated?
From: Ry Nohryb on
On Jun 6, 12:15 pm, M A Hossain Tonu <maht...(a)gmail.com> wrote:
> (...)
> > JSON is a means of describing objects using javascript object literal
> > syntax, nothing more.

A susbset of.

> 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'}";
> (...)

Note that the text into that jsonString is *not* valid JSON. It should
be:

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

IOW: "javascript object literal syntax" !== JSON
--
Jorge.