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/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