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.
> function F() {}
> F.prototype = Person.prototype;
> Admin.prototype = new F();
Person.prototype is empty in your example. This instruction won't build a
prototype chain between Admin and Person, only between Admin and
Person.prototype, which is {}.
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. and I'm afraid
I don't know what Anatoly meant by "currying the prototype".
I'd like to point to the excellent serie of articles beginning with the
third post of http://dailyjs.com/tags.html#lmaf for an in-depth exploration
of prototypal inheritance.
regards,
Cedric
2011/11/23 Scott Sauyet <[email protected]>
> Cédric Bertolini wrote:
> > Scott Sauyet wrote:
>
> >> I agree that it's a useful pattern. I often use something similar.
> >> But what do you mean by currying the prototype? The prototype is an
> >> object, and not a function.
>
> > Scott, as an object, the prototype is often returned by a function
> (called
> > or newed). Let's say I want to create a new constructor "Admin" that
> > inherits from Person :
> >
> > var Admin = (function() {
> > var prototype = Person.curry('admin');
> > var constructor = function () { };
> > constructor.prototype = new prototype;
> > return constructor;
> > })();
> > [ ... ]
>
> It's an interesting idea. Some here would object to using the result
> of calling a constructor function as a prototype. They have some real
> reasons, but it's not a problem that has usually bothered me too
> much. But I am worried about some rigidity this would confer on your
> types. If you have a Person constructor:
>
> var Person = function(first, last) {
> this.firstName = first;
> this.lastName = last;
> // ...
> }
>
> if you later decide you need to extend it by your technique, you would
> have to add the new type parameter first, in order to take advantage
> of currying. That means you would have to adjust every call to that
> constructor. If you did this using an Object.create method or the old
>
> function F() {}
> F.prototype = Person.prototype;
> Admin.prototype = new F();
>
> technique, then you could simply have the constructor invoke the
> parent constructor if appropriate:
>
> function Admin(first, last) {
> Person.call(this, first, last); // use `super` if your
> inheritance mechanism supplies it.
> // admin stuff here
> }
>
> This sort of class can be created without redesigning Person.
> Currying would not allow that; you'd have to design it in up front.
>
> -- Scott
>
> --
> To view archived discussions from the original JSMentors Mailman list:
> http://www.mail-archive.com/[email protected]/
>
> To search via a non-Google archive, visit here:
> http://www.mail-archive.com/[email protected]/
>
> To unsubscribe from this group, send email to
> [email protected]
>
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]