Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > How to express DAO class model in JS?

Reply
Thread Tools

How to express DAO class model in JS?

 
 
nick
Guest
Posts: n/a
 
      01-26-2010
On Jan 26, 1:37*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
> err_ wrote:
> > Pointy,

>
> Who, error?


bah, google groups is a pain.

>
> > (I'm sure you probably know this but for the record)

>
> Yes, I do.
>
> > the problem with moving the privates out of the constructor
> > is they act sort of "static" if you put them out there...
> > They act like private statics, which sounds like a contradiction,
> > but look:

>
> ISTM that you don't know what you are talking about.
>


That's okay, I'm confident that I do


> > this.TestA = (function() {

>
> > * // private outside of ctor
> > * var _myPvt = ' do not touch ';

>
> > * // ctor
> > * function TestA (newPvt)
> > * {
> > * * if (newPvt !== undefined)
> > * * * _myPvt = newPvt;
> > * }
> > [...]

>
> Non sequitur. *You *modified* the constructor here so that it modified the
> "private" variable; my code does not do that. *


I could have modified it by adding a setter to achieve the same thing.

> That leaves as the only
> inherent problem with moving the prototype property (not variable)
> assignment out of the constructor, that the prototype property value is not
> reset on construction. *


That was exactly my point -- the value of those variables is shared
between every "instance" of the "class."

this.TestA = (function() {
var _myPvt = ' do not touch ';
function _getMyPvt()
{
return _myPvt;
}
function _setMyPvt(v)
{
_myPvt = v;
}
function TestA ()
{
/*
* buy the assignment with a type test here, or move
* it out of the constructor, if you want
*/
TestA.prototype.getMyPvt = _getMyPvt;
TestA.prototype.setMyPvt = _setMyPvt;
}
/* Assign this like getMyPvt() in the constructor if you want */
TestA.prototype.talk = function() {
window.alert(this.getMyPvt());
};
return TestA;
})();


var foo = new TestA();

foo.talk();
foo.setMyPvt(" whee ");

var bar = new TestA();
bar.talk(); // "whee"

bar.setMyPvt(" whoo ");

foo.talk(); // "whoo"

....
So saying these are anything like private class properties in a
'normal' OO language is misleading. Not that you said that, of course.

-- Nick
 
Reply With Quote
 
 
 
 
Dmitry A. Soshnikov
Guest
Posts: n/a
 
      01-26-2010
On Jan 26, 9:18*pm, err_ <nick.m...@gmail.com> wrote:
> On Jan 26, 12:38*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
> wrote:
>
>
>
> > Scott Sauyet wrote:
> > > How about if you provide the function in your base class, then
> > > override it in your extension?

>
> > > * * this.TestA = (function(){
> > > * * * // constructor
> > > * * * function TestA ()
> > > * * * {
> > > * * * * var myPvt = ' do not touch ';
> > > * * * * TestA.prototype.talk = function(){alert(this.getMyPvt())}
> > > * * * * TestA.prototype.getMyPvt = function(){return myPvt};
> > > * * * }
> > > * * * return TestA;
> > > * * })();

>
> > > I think this would behave the way you want, but it's not very clean.

>
> Scott, that's something I hadn't thought of... it's not exactly what I
> want but it's better than that eval hack for sure... thanks
>
>
>
> > And there are no classes. *Besides, the talk() prototype method does not
> > need to be defined in the constructor, and the getMyPvt() prototype method
> > needs to be defined only once.

>
> > * this.TestA = (function() {
> > * * var _myPvt = ' do not touch ';

>
> > * * function _getMyPvt()
> > * * {
> > * * * /* or hard-code the value here */
> > * * * return _myPvt;
> > * * }

>
> > * * function TestA ()
> > * * {
> > * * * /*
> > * * * ** buy the assignment with a type test here, or move
> > * * * ** it out of the constructor, if you want
> > * * * **/
> > * * * TestA.prototype.getMyPvt = _getMyPvt;
> > * * }

>
> > * * /* Assign this like getMyPvt() in the constructor if you want */
> > * * TestA.prototype.talk = function() {
> > * * * window.alert(this.getMyPvt());
> > * * };

>
> > * * return TestA;
> > * })();

>
> Pointy, (I'm sure you probably know this but for the record) the
> problem with moving the privates out of the constructor is they act
> sort of "static" if you put them out there... They act like private
> statics, which sounds like a contradiction, but look:
>
> this.TestA = (function() {
>
> * // private outside of ctor
> * var _myPvt = ' do not touch ';
>
> * // ctor
> * function TestA (newPvt)
> * {
> * * if (newPvt !== undefined)
> * * * _myPvt = newPvt;
> * }
>
> * // methods here or in ctor
> * TestA.prototype.talk = function()
> * {
> * * window.alert(this.getMyPvt());
> * };
> * TestA.prototype.getMyPvt = function()
> * {
> * * return _myPvt;
> * };
> * TestA.prototype.setMyPvt = function(v)
> * {
> * * _myPvt = v; return this;
> * };
> * return TestA;
>
> })();
>
> var foo = new TestA();
>
> foo.talk(); *// do not touch
>
> var bar = new TestA('lalala');
>
> bar.talk(); // lalala
>
> foo.talk(); // lalala
>
> var baz = new TestA();
>
> baz.talk(); // lalala
>
>


Anyway, described pattern won't allow to realize the complete getter/
setter for encapsulated data - it's just useful for getter, setter
from the other object will rewrite the same shared value which is in
scope chain of function allowed for both objects.

So this is just complicated way of more clear declaration of *shared*
encapsulated entities in helper execution context for creating
prototype object (but not the constructor function):

function A(x) {
this.x = x || 100;
}

A.prototype = (function () {

var _someSharedVar = 500;

function _someHelper() {
alert('internal helper: ' + _someSharedVar);
}

function method1() {
alert('method1: ' + this.x);
}

function method2() {
alert('method2: ' + this.x);
_someHelper();
}

return {
constructor: A,
method1: method1,
method2: method2
};

})();

var a = new A(10);
var b = new A(20);

a.method1(); // method1: 10
a.method2(); // method2: 10, internal helper: 500

b.method1(); // method1: 20
b.method2(); // method2: 20, internal helper: 500

/ds
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
simpler over view on dao: a functional logic solver with builtinparsing power, and dinpy, the sugar syntax for dao in python Simeon Chaos Python 0 11-08-2011 05:34 AM
Convert Java Model to Java Model without XML erinbot@gmail.com Java 1 10-06-2006 09:00 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
asp.net and dao? mirek ASP .Net 2 08-06-2003 10:54 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57