Scott, Cedric,

good discussion, I was working on something, could get involved earlier.  

I can't remember what I meant by that comment, I am sorry. Looking back at it, 
I think I liked that pattern (below) for being able to generate new instances 
of various objects by passing them into that function and getting back new 
Objects. Why the curry? I think in order to execute the factory in the context 
of the original object passed in, but I am not sure that it makes sense in 
every case.  

var MyClass = (function() { var prototype = { /* shared attributes */ }; var 
constructor = function () {  
/* instance properties and bootstrapping */  
};
constructor.prototype = prototype; return constructor; })();

Regardless, another creation pattern I use is:

var ObjInheritor = function(_params) {
    ObjParent.apply(this, [_params]);

};
ObjInheritor.prototype = Object.create(ObjParent.prototype);
ObjInheritor.prototype.constructor = ObjInheritor;


this gives me a prototype chain, and the ability to create complex constructors 
for my objects. I admit that this is closer to classical inheritance but with 
prototypes, but it works well for creating an object hierarchy for us to work 
with that provides much opportunity to reuse common functionality for all 
children components.  

--  
Anatoly Geyfman
http://www.geyfman.net


On Wednesday, November 23, 2011 at 12:40 PM, Scott Sauyet wrote:

> Cédric Bertolini wrote:
> > Well, I had never seen this pattern before:
> > function Admin(first, last)  {
> >     Person.call(this, first, last);
> > }
> >  
> > It's interesting, but you're making a lot of assumptions about Person. You
> > can only use that if you thightly control Person and Admin.
> >  
>  
>  
> Not exactly. You would only write this in writing Admin, so
> presumably you control that. And you would have to thoroughly
> understand the implications of calling the Person constructor. But
> you wouldn't have to control the Person constructor at all.
> Obviously, if it changed, Admin might also need to change, but that's
> true of any change to any public API.
>  
>  
>  
> > >   function F() {}
> > >   F.prototype = Person.prototype;
> > >   Admin.prototype = new F();
> > >  
> >  
> >  
> > Person.prototype is empty in your example.
>  
> I didn't bother to write anything, but it exists, and if you like, we
> can add:
>  
> Person.prototype.greet = function() {
> alert ("Hello, " + this.firstName);
> }
>  
> This would be inherited by all Admins as well as any other Person
> objects.
>  
>  
> > This instruction won't build a
> > prototype chain between Admin and Person, only between Admin and
> > Person.prototype, which is {}.
> >  
>  
>  
> Really by definition, a prototype chain exists between
> prototypes. :-)
>  
>  
> > Anyway, you are right in saying that currying the constructor doesn't build
> > a prototype chain between Admin and Person. But going further in this
> > direction will only lead us to recreate Class.create and al.
> >  
>  
>  
> I didn't say this. Honestly, because of my other objections, I hadn't
> looked closely enough at it to notice. But you're quite right that
> with the code as presented, object constructed from Admin would not
> inherit from Person. It would not be hard to fix, though:
>  
> var Admin = (function() {
> var F = function() {};
> F.prototype = Person.prototype;
> var constructor = Person.curry('admin');
> constructor.prototype = new F();
> return constructor;
> })();
>  
> var admin = new Admin("Jane", "Doe");
>  
> This fixes the inheritance issue, but it still requires Person to be
> designed for extension by things like Admin. And if you need
> additional parameters for ExecutiveAdmin, you'd have to update Admin
> and Person.
>  
> > I'm afraid I don't know what Anatoly meant by "currying the prototype".
>  
> Me neither. Anatoly, are you still around?
>  
> -- Scott
>  
> --  
> To view archived discussions from the original JSMentors Mailman list: 
> http://www.mail-archive.com/jsmentors@jsmentors.com/
>  
> To search via a non-Google archive, visit here: 
> http://www.mail-archive.com/jsmentors@googlegroups.com/
>  
> To unsubscribe from this group, send email to
> jsmentors+unsubscr...@googlegroups.com 
> (mailto:jsmentors+unsubscr...@googlegroups.com)
>  
>  
>  


-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/jsmentors@jsmentors.com/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/jsmentors@googlegroups.com/

To unsubscribe from this group, send email to
jsmentors+unsubscr...@googlegroups.com

Reply via email to