Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > JSON Style JavaScript Class/Object declaration

Reply
Thread Tools

JSON Style JavaScript Class/Object declaration

 
 
Asen Bozhilov
Guest
Posts: n/a
 
      06-06-2010
Justin Johnson wrote:

> 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());


If someone need copy of a singleton object there is design mistake.
That is contradiction with conception of a singleton pattern. Your
defense is not enough for unreasonable development and should not. If
there is a developer who misunderstand your concept, he always can
broke your code.


 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      06-06-2010
M A Hossain Tonu <(E-Mail Removed)> writes:

>> 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.a = 4;
> console.log(a.a, b.a, c.a);
>
> outputs 4 4 4 they are using same memory...


No, it doesn't. The b variable holds a reference to a different object,
and it writes 3,4,3.

> cause they are copy not an unique instance....so where is the
> singelton pattern violated?


The "singleton pattern" makes no sense in a classless language.
It means that you can only create one instance of a specific class.
It has no meaning when there are no classes.
What would it be that you could only create on instance of?

/L 'You could do it with host objects, but then, you can do
anything with those'
--
Lasse Reichstein Holst Nielsen
'Javascript frameworks is a disruptive technology'

 
Reply With Quote
 
 
 
 
VK
Guest
Posts: n/a
 
      06-06-2010
On Jun 6, 2:15*pm, M A Hossain Tonu <(E-Mail Removed)> wrote:
> Any suggestion will be highly appreciable.


The further is not a group consensus but my own private opinion.

[1] Of course there are classes in JavaScript. Like in any OO language
there are classes of things and instances of those classes of things.
Semi-hysterical "there are no classes in JavaScript!!" and tabooed
word "class" itself comes from the JavaScript childhood. Then it was a
protection mechanics against "real languages" that were laughing over
"that toy language". So the story was that "I am not primitive, I am
different! Very very different and hard to be understood by outsiders.
I am the Wrrrld's Most Misunderstood Programming Language! (c)2001
Crockford http://javascript.crockford.com/javascript.html "

For the same reason the prototype-based inheritance, that is as simple
as a moo-cow, was often presented nearly as a ultimate lore with a
bunch of convoluted samples proving its complex nature.

The childhood is way over and this "protection behavior" is not
needed. There are classes and instances in JavaScript just like
everywhere else. The core difference from C-like languages is that
there we have the strict specialization: there are objects that create
new objects (classes) and there are object that do the actual job the
program is created for (instances). They are not interchangeable: one
cannot use a class to calculate 2*2=? and one cannot use an instance,
calculating 2*2=?, as a class.
In JavaScript constructor is the same first order function so
theoretically it can be used as an instance constructor or just be
called as a function. This polymorphism is purely theoretical though:
it can't be a descent code with function Foo being used in either way
in the same program, like this
var obj = new Foo();
and then some later
Foo();

[2] Your article is rather confusing by its layout. Basically you are
describing some elements of CC scope management, namely the creation
of protected properties and privileged methods. Other words instance
properties that can be accessed only over particular instance methods.
It is a commonly used practice now, but it has nothing to do with
singletons. It can be N instances of the same class with protected
properties and privileged methods. It can be a singleton without any
protected properties and privileged methods. These are simply
unrelated things. And most definitely neither first nor second has any
relation with JSON data-interchange format. What you really wrote, in
proper JavaScript terms, is "Using privileged methods in JavaScript:
how to and benefits". And if writing about Cornford-Crockford scope
management aspects it may be useful to read the original papers:
"Private Members in JavaScript" by Douglas Crockford (2001)
http://www.crockford.com/javascript/private.html
and
"Private Static Members in JavaScript" by Richard Cornford (2003)
The article is gone but still available over Wayback Machine:
http://web.archive.org/web/200312050...te_static.html

A nice summary can be found in this group archives in the discussion
"closures, what are they good for?" (April-May 2003)
http://groups.google.com/group/comp....3cd655ad56fe91

[3] In reference to singleton it would be a whole different article
and a different discussion. If you are interested in singletons and
not in CC scope management, let's talk about singletons.
 
Reply With Quote
 
M A Hossain Tonu
Guest
Posts: n/a
 
      06-06-2010
The problem with that clone method, that if you have sub objects
within the obj, their references will be cloned, and not the values of
every sub object.

On Jun 6, 5:20*pm, Lasse Reichstein Nielsen <(E-Mail Removed)>
wrote:
> M A Hossain Tonu <(E-Mail Removed)> writes:
>
>
>
> >> 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.a = 4;
> > console.log(a.a, b.a, c.a);

>
> > outputs 4 4 4 they are using same memory...

>
> No, it doesn't. The b variable holds a reference to a different object,
> and it writes 3,4,3.
>
> > cause they are copy not an unique instance....so where is the
> > singelton pattern violated?

>
> The "singleton pattern" makes no sense in a classless language.
> It means that you can only create one instance of a specific class.
> It has no meaning when there are no classes.
> What would it be that you could only create on instance of?
>
> /L 'You could do it with host objects, but then, you can do
> * * anything with those'
> --
> Lasse Reichstein Holst Nielsen
> *'Javascript frameworks is a disruptive technology'


 
Reply With Quote
 
M A Hossain Tonu
Guest
Posts: n/a
 
      06-06-2010
Thank you very much for your valuable suggestion. I am altering and
filling the post.

Tonu


On Jun 6, 5:27*pm, VK <(E-Mail Removed)> wrote:
> On Jun 6, 2:15*pm, M A Hossain Tonu <(E-Mail Removed)> wrote:
>
> > Any suggestion will be highly appreciable.

>
> The further is not a group consensus but my own private opinion.
>
> [1] Of course there are classes in JavaScript. Like in any OO language
> there are classes of things and instances of those classes of things.
> Semi-hysterical "there are no classes in JavaScript!!" and tabooed
> word "class" itself comes from the JavaScript childhood. Then it was a
> protection mechanics against "real languages" that were laughing over
> "that toy language". So the story was that "I am not primitive, I am
> different! Very very different and hard to be understood by outsiders.
> I am the Wrrrld's Most Misunderstood Programming Language! (c)2001
> Crockfordhttp://javascript.crockford.com/javascript.html"
>
> For the same reason the prototype-based inheritance, that is as simple
> as a moo-cow, was often presented nearly as a ultimate lore with a
> bunch of convoluted samples proving its complex nature.
>
> The childhood is way over and this "protection behavior" is not
> needed. There are classes and instances in JavaScript just like
> everywhere else. The core difference from C-like languages is that
> there we have the strict specialization: there are objects that create
> new objects (classes) and there are object that do the actual job the
> program is created for (instances). They are not interchangeable: one
> cannot use a class to calculate 2*2=? and one cannot use an instance,
> calculating 2*2=?, as a class.
> In JavaScript constructor is the same first order function so
> theoretically it can be used as an instance constructor or just be
> called as a function. This polymorphism is purely theoretical though:
> it can't be a descent code with function Foo being used in either way
> in the same program, like this
> *var obj = new Foo();
> and then some later
> *Foo();
>
> [2] Your article is rather confusing by its layout. Basically you are
> describing some elements of CC scope management, namely the creation
> of protected properties and privileged methods. Other words instance
> properties that can be accessed only over particular instance methods.
> It is a commonly used practice now, but it has nothing to do with
> singletons. It can be N instances of the same class with protected
> properties and privileged methods. It can be a singleton without any
> protected properties and privileged methods. These are simply
> unrelated things. And most definitely neither first nor second has any
> relation with JSON data-interchange format. What you really wrote, in
> proper JavaScript terms, is "Using privileged methods in JavaScript:
> how to and benefits". And if writing about Cornford-Crockford scope
> management aspects it may be useful to read the original papers:
> * "Private Members in JavaScript" by Douglas Crockford (2001)
> *http://www.crockford.com/javascript/private.html
> and
> * "Private Static Members in JavaScript" by Richard Cornford (2003)
> * The article is gone but still available over Wayback Machine:
> *http://web.archive.org/web/200312050...totes.demon.co....
>
> A nice summary can be found in this group archives in the discussion
> * "closures, what are they good for?" (April-May 2003)
> *http://groups.google.com/group/comp....wse_frm/thread....
>
> [3] In reference to singleton it would be a whole different article
> and a different discussion. If you are interested in singletons and
> not in CC scope management, let's talk about singletons.


 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      06-06-2010
On Jun 6, 2:47*pm, Asen Bozhilov <(E-Mail Removed)> wrote:
> If someone need copy of a singleton object there is design mistake.


One can clone an object, but it is not possible to clone a closure
state, so it is not possible to get access to the actual protected
member values other way than using privileged methods. That is the
core idea of CC, see
"Private Members in JavaScript" by Douglas Crockford (2001)
http://www.crockford.com/javascript/private.html

Again, OP used a misleading title for the article, it is all about
protected members and privileged methods. Singletons and the
"singleton pattern" has no relation with the topic of discussion.
Singletons in JavaScript are mainly used at the script start stage,
when a bunch of environment testing and instantiation has to be done.
To keep the namespace clean afterworlds, it is often wrapped into
anonymous function:

(function(){
// DO stuff
})();

or equivalent:

(function(){
// DO stuff
}());

If one needs to get a permanent single instance of something and then
to eliminate the possibility of creating new instances, then by KISS
the simplest is just to eliminate the constructor:

var foo = new Foo();
Foo = null;
foo.constructor = null;

JavaScript function being called as constructor may return anything
instead of new instance, as long as it is typeof object. That allows
to create singletons, doubletons, tripletons etc. but I hope I will
never see it in a real code.

 
Reply With Quote
 
Asen Bozhilov
Guest
Posts: n/a
 
      06-06-2010
VK wrote:
> Asen Bozhilov wrote:
>
> > If someone need copy of a singleton object there is design mistake.

>
> One can clone an object, but it is not possible to clone a closure
> state, so it is not possible to get access to the actual protected
> member values other way than using privileged methods.


That is depend on implementation, some implementations provide access
to internal [[Scope]] property. For example Rhino, there is
`__parent__' property which is mimic of [[Scope]]. So I can get
reference to VO associated with execution context in which some
Function Declaration/Expression is evaluated.

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 aVO = a.getA.__parent__;
for (var i in aVO) {
print(i);
}

The output in Rhino 1.7 from code above is:

arguments
_a
_b


 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      06-06-2010
On Jun 6, 6:52*pm, Asen Bozhilov <(E-Mail Removed)> wrote:
> That is depend on implementation, some implementations provide access
> to internal [[Scope]] property. For example Rhino, there is
> `__parent__' property which is mimic of [[Scope]]. So I can get
> reference to VO associated with execution context in which some
> Function Declaration/Expression is evaluated.
>
> 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 aVO = a.getA.__parent__;
> for (var i in aVO) {
> * * print(i);
>
> }
>
> The output in Rhino 1.7 from code above is:
>
> arguments
> _a
> _b


That is an important info, thank you. So Rhino breaks any
encapsulation attempts and cannot be used for some commercial projects
where encapsulation is really a must. Overall the __properties__ like
__parent__ or __proto__ are for the *engine* testing only. Why
Netscape and now Mozilla doesn't lock it in release versions and
prefers to compromise the security and the stability - is a dark
misery. Too busy with getter/setter crap and with breaking backward
compatibility. Just a guess...



 
Reply With Quote
 
Dmitry A. Soshnikov
Guest
Posts: n/a
 
      06-06-2010
On 06.06.2010 19:01, VK wrote:
> On Jun 6, 6:52 pm, Asen Bozhilov<(E-Mail Removed)> wrote:
>> That is depend on implementation, some implementations provide access
>> to internal [[Scope]] property. For example Rhino, there is
>> `__parent__' property which is mimic of [[Scope]]. So I can get
>> reference to VO associated with execution context in which some
>> Function Declaration/Expression is evaluated.
>>
>> 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 aVO = a.getA.__parent__;
>> for (var i in aVO) {
>> print(i);
>>
>> }
>>
>> The output in Rhino 1.7 from code above is:
>>
>> arguments
>> _a
>> _b

>
> That is an important info, thank you. So Rhino breaks any
> encapsulation attempts and cannot be used for some commercial projects
> where encapsulation is really a must.


Don't understand an encapsulation incorrectly. This is less about
security, and more about increasing of an abstraction

For details: <URL: http://bit.ly/am38pU>
About __parent__: <URL: http://bit.ly/9L1hWe>

Dmitry.


 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      06-06-2010
On Jun 6, 8:02*pm, "Dmitry A. Soshnikov" <(E-Mail Removed)>
wrote:
> Don't understand an encapsulation incorrectly. This is less about
> security, and more about increasing of an abstraction


Well, JavaScript never cared about abstraction and other theoretical
stuff, so the only way it could understand encapsulation is by
interpreting it in access restriction / data security terms Which
is IMHO the original purpose of encapsulation in the first case. The
global abstraction logic is secondary, to make CS students to use
encapsulation instinctively by the end of the university.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lib to generate XML/JSON[P] output from a DTD/XSD/JSON Schema/etc Acácio Centeno Python 1 02-15-2013 07:34 AM
I am facing an issue while decoding json string using json.loads sajuptpm Python 2 12-28-2012 07:16 AM
[ANN] Security Fix json-1.1.7 for json_pure and json gems Florian Frank Ruby 0 06-30-2009 05:18 PM
"JSON for ASP" at json.org Tuğrul Topuz ASP General 1 06-27-2008 11:37 PM
Need help with Style conversion from Style object to Style key/value collection. Ken Varn ASP .Net Building Controls 0 04-26-2004 07:06 PM



Advertisments