From: Ry Nohryb on
On May 7, 8:27 pm, Ry Nohryb <jo...(a)jorgechamorro.com> wrote:
> On May 7, 7:35 pm, Ry Nohryb <jo...(a)jorgechamorro.com> wrote:
>
>
>
>
>
> > On May 7, 2:54 pm, "Michael Haufe (\"TNO\")"
>
> > <t...(a)thenewobjective.com> wrote:
> > > On May 7, 2:27 am, Garrett Smith <dhtmlkitc...(a)gmail.com> wrote:
>
> > > > Hi Everybody,
>
> > > > What is your most challenging interview question?
>
> > > Q: How would you implement a constructor that inherits a method from
> > > it's prototype that has access to it's instance private variables? In
> > > other words if I had:
>
> > > function Foo(a){
> > >     ...
> > >     var _foo = a
> > >     ...
>
> > > }
>
> > > how would you use prototypical inheritance so that the following is
> > > possible:
>
> > > var obj1 = new Foo("one"),
> > >     obj2 = new Foo("two");
>
> > > obj1.thing() //"one"
> > > obj2.thing() //"two"
>
> > I'll bite... :-)
>
> > var clase= (function claseBuilder () {
> >   var instances= [];
> >   var getters= [];
> >   var clase= {};
>
> >   clase.getPrivate= function () {
> >     return getters[instances.indexOf(this)]();
> >   };
>
> >   clase.pushGetter= function (instance, getter) {
> >     instances.push(instance);
> >     getters.push(getter);
> >   };
>
> >   return clase;
>
> > })();
>
> > function subclase (private) {
> >   o= {};
> >   o.__proto__= clase;
> >   clase.pushGetter(o, function () { return private });
> >   return o;
>
> > }
>
> > var a= subclase(33);
> > var b= subclase(27);
>
> > [a.hasOwnProperty("getPrivate"), a.hasOwnProperty("getPrivate")]
> > --> [ false, false ]
> > [a.getPrivate(), b.getPrivate()]
> > --> [ 33, 27 ]
>
> > --
> > Jorge.
>
> Version 2:
>
> var subClase;
> var clase= (function claseBuilder (instances, getters, clase) {
>   clase.getPrivate= function () {
>     return getters[instances.indexOf(this)]();
>   };
>   subClase= function subClaseBuilder (private, o) {
>     (o= {}).__proto__= clase;
>     instances.push(o);
>     getters.push(function () { return private });
>     return o;
>   };
>   return clase;
>
> })([], [], {});
>
> var a= subClase(33);
> var b= subClase(27);
>
> [a.hasOwnProperty("getPrivate"), b.hasOwnProperty("getPrivate")]
> --> [ false, false ]
> [a.getPrivate(), b.getPrivate()]
> --> [ 33, 27 ]
> --
> Jorge.

Version 3:

This one, however, duplicates the private values, which is not a Good
Idea™, i.e. if private were a 150MB string... BAD.

var subClase;
var clase= (function claseBuilder (instances, values, clase) {
clase.getPrivate= function () {
return values[instances.indexOf(this)];
};
subClase= function subClaseBuilder (private, o) {
(o= {}).__proto__= clase;
instances.push(o);
values.push(private);
return o;
};
return clase;
})([], [], {});


var a= subClase(33);
var b= subClase({});


[a.hasOwnProperty("getPrivate"), b.hasOwnProperty("getPrivate")]
--> [ false, false ]
[a.getPrivate(), b.getPrivate()]
--> [ 33, {} ]
--
Jorge.
From: VK on
On May 7, 10:35 pm, Ry Nohryb <jo...(a)jorgechamorro.com> wrote:
> On May 7, 8:27 pm, Ry Nohryb <jo...(a)jorgechamorro.com> wrote:
>
>
>
> > On May 7, 7:35 pm, Ry Nohryb <jo...(a)jorgechamorro.com> wrote:
>
> > > On May 7, 2:54 pm, "Michael Haufe (\"TNO\")"
>
> > > <t...(a)thenewobjective.com> wrote:
> > > > On May 7, 2:27 am, Garrett Smith <dhtmlkitc...(a)gmail.com> wrote:
>
> > > > > Hi Everybody,
>
> > > > > What is your most challenging interview question?
>
> > > > Q: How would you implement a constructor that inherits a method from
> > > > it's prototype that has access to it's instance private variables? In
> > > > other words if I had:
>
> > > > function Foo(a){
> > > >     ...
> > > >     var _foo = a
> > > >     ...
>
> > > > }
>
> > > > how would you use prototypical inheritance so that the following is
> > > > possible:
>
> > > > var obj1 = new Foo("one"),
> > > >     obj2 = new Foo("two");
>
> > > > obj1.thing() //"one"
> > > > obj2.thing() //"two"
>
> > > I'll bite... :-)
>
> > > var clase= (function claseBuilder () {
> > >   var instances= [];
> > >   var getters= [];
> > >   var clase= {};
>
> > >   clase.getPrivate= function () {
> > >     return getters[instances.indexOf(this)]();
> > >   };
>
> > >   clase.pushGetter= function (instance, getter) {
> > >     instances.push(instance);
> > >     getters.push(getter);
> > >   };
>
> > >   return clase;
>
> > > })();
>
> > > function subclase (private) {
> > >   o= {};
> > >   o.__proto__= clase;
> > >   clase.pushGetter(o, function () { return private });
> > >   return o;
>
> > > }
>
> > > var a= subclase(33);
> > > var b= subclase(27);
>
> > > [a.hasOwnProperty("getPrivate"), a.hasOwnProperty("getPrivate")]
> > > --> [ false, false ]
> > > [a.getPrivate(), b.getPrivate()]
> > > --> [ 33, 27 ]
>
> > > --
> > > Jorge.
>
> > Version 2:
>
> > var subClase;
> > var clase= (function claseBuilder (instances, getters, clase) {
> >   clase.getPrivate= function () {
> >     return getters[instances.indexOf(this)]();
> >   };
> >   subClase= function subClaseBuilder (private, o) {
> >     (o= {}).__proto__= clase;
> >     instances.push(o);
> >     getters.push(function () { return private });
> >     return o;
> >   };
> >   return clase;
>
> > })([], [], {});
>
> > var a= subClase(33);
> > var b= subClase(27);
>
> > [a.hasOwnProperty("getPrivate"), b.hasOwnProperty("getPrivate")]
> > --> [ false, false ]
> > [a.getPrivate(), b.getPrivate()]
> > --> [ 33, 27 ]
> > --
> > Jorge.
>
> Version 3:
>
> This one, however, duplicates the private values, which is not a Good
> Idea™, i.e. if private were a 150MB string... BAD.
>
> var subClase;
> var clase= (function claseBuilder (instances, values, clase) {
>   clase.getPrivate= function () {
>     return values[instances.indexOf(this)];
>   };
>   subClase= function subClaseBuilder (private, o) {
>     (o= {}).__proto__= clase;
>     instances.push(o);
>     values.push(private);
>     return o;
>   };
>   return clase;
>
> })([], [], {});
>
> var a= subClase(33);
> var b= subClase({});
>
> [a.hasOwnProperty("getPrivate"), b.hasOwnProperty("getPrivate")]
> --> [ false, false ]
> [a.getPrivate(), b.getPrivate()]
> --> [ 33, {} ]
> --
> Jorge.

Holly Mother of Lord, can you just decide if you need static or
impersonated methods and where?! :-) Really, it is a design time human
decision - not a runtime engine problem of any kind. :-) :-|

C++'ers and Java'ers used to treat the memory as an endless sh**, but
JavaScript provides and expects more nice and professional treatment,
moreover it requires almost zero efforts.
With the Cornford-Crockford scope management (a.k.a. CC scope
management, the name order is alphabetical) you are making right now
the crucial question for each method is: "I do not care about the
previous call state for this method for this class instance or do I?".
In the first case it is static in VBA sense. 99% of your methods
should be static in the mentioned sense. The opposite indicates a
wrong programming paradigm. Otherwise it may be crucial to know the
previous onExit state of the method for that particular instance or
even the whole instance state at the moment onExit from that method
(impersonated method). It is very memory costly and it must be used
only when you really need it - not everywhere as a common programming
paradigm.
From: Swifty on
On Fri, 07 May 2010 00:27:46 -0700, Garrett Smith
<dhtmlkitchen(a)gmail.com> wrote:

>What is your most challenging interview question?

Him: What's the first thing you do after writing a program?
Me: Compile it.
Him (tetchily): Yes, yes, but AFTER that?
Me: I test it.
Him: Why?
Me: In case I made any mistakes...

Here it comes...

Him (eyes narrowed): So! You make mistakes, do you?

I'd like to say that at this point I invented the saying "To err is
human. To really foul things up, you need a computer", but that is
just in my dreams, but my answer must have been adequate.

The original interviewer had been his wife. After about 10 minutes,
Phyll called her husband (Him: aka Geoff) and I heard her say "We've
got a bright one here; come and ask him some questions that he can't
answer!"

I got the job, and Phyll and Geoff remain good friends of mine.

--
Steve Swift
http://www.swiftys.org.uk/swifty.html
http://www.ringers.org.uk
From: Scott Sauyet on
nick wrote:
> Argh! "Its" ... "its" ... "its."
>
> It's a declension. Not possessive, not a contraction. Like "his" or
> "her," it needs no apostrophe.

This mistake has it's opponents but really its no big deal. :-)

--
Scott
From: nick on
On May 7, 3:39 pm, Scott Sauyet <scott.sau...(a)gmail.com> wrote:
> nick wrote:
> > Argh! "Its" ... "its" ... "its."
>
> > It's a declension. Not possessive, not a contraction. Like "his" or
> > "her," it needs no apostrophe.
>
> This mistake has it's opponents but really its no big deal.  :-)

Ooh, that was dirty.

--
Never make misteaks!