From: Thomas 'PointedEars' Lahn on 7 May 2010 16:20 Michael Haufe ("TNO") wrote: > "Michael Haufe (\"TNO\")" wrote: >> On May 7, 2:27 am, Garrett Smith <dhtmlkitc...(a)gmail.com> wrote: >> > 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" > > An addendum for those playing at home and want to try: the "thing()" > method should be declared only once. If you are saying that there are trick questions in job interviews, I agree. If instead you are implying that there is a solution to the problem as stated (spelling errors notwithstanding), you should post your solution so that one can assess whether the problem is properly stated and your solution makes sense. PointedEars -- Use any version of Microsoft Frontpage to create your site. (This won't prevent people from viewing your source, but no one will want to steal it.) -- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
From: "Michael Haufe ("TNO")" on 7 May 2010 16:56 On May 7, 12:27 pm, John G Harris <j...(a)nospam.demon.co.uk> wrote: > I'd point out, politely I hope, that you wrote the question without > enough proof-reading :-) True enough. > Although a constructor can inherit a method from it's [sic] prototype > there isn't much point in its doing so, and there's no way a > constructor's method can access an instance's data before that data has > been created. Not in the normal sense you are thinking, no. > Also, I'd ask what you mean by 'private'. Not directly accessible.
From: "Michael Haufe ("TNO")" on 7 May 2010 16:58 On May 7, 1:06 pm, nick <nick...(a)fastmail.fm> wrote: > On May 7, 8:54 am, "Michael Haufe (\"TNO\")" > > <t...(a)thenewobjective.com> wrote: > > 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: > > Argh! "Its" ... "its" ... "its." > > It's a declension. Not possessive, not a contraction. Like "his" or > "her," it needs no apostrophe. > > Sorry, Michael, but this is like the 20th time I've seen this mistake > on cljs in the last 24 hours or so... figured it was time to say > something. Fair criticism, I should have proof read.
From: Mel Smith on 8 May 2010 00:50 Interview question: What's the difference between a Duck ?
From: David Mark on 8 May 2010 00:56
Ry Nohryb 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, {} ] > -- Strike #3. Get out of my office. :) Seriously, instead of pretending IE does not exist, why not pretend the same of things like __proto__? |