constructing vs prototyping

Discussion in 'Javascript' started by cainlevy, Apr 20, 2005.

  1. cainlevy

    cainlevy Guest

    Hey all,

    What are the pros and cons of defining methods in the constructor vs
    through the prototype? For example:

    Constructing:
    -------------
    function MyObj()
    {
    this.MyMethod = function() {};
    }

    Prototyping:
    ------------
    function MyObj() {}
    MyObj.prototype.MyMethod = function() {};

    Currently, I'm of the mindset that the former method is preferable in
    all situations except where you don't have access to the constructor
    (e.g. adding to Object or Array or whatnot). Part of my reasoning is
    personal preference for how the code looks, but what really got me
    started coding with the former method was my switch to using closures.
    I use closures to keep an object reference even in event handlers, by
    beginning every object with a "var self = this;" line. The problem with
    prototype functions being that they don't have the closure.

    So,

    -------
    function MyObj()
    {
    var self = this;
    self.msg = 'Hello World';
    self.toString = function() {return self.msg};
    }
    alert(new MyObj);
    -------

    works, but

    -------
    function MyObj()
    {
    var self = this;
    self.msg = 'Hello World';
    }
    MyObj.prototype.toString = function() {return self.msg};
    alert(new MyObj);
    -------

    does not. From that basic advantage of defining a method in the
    constructor, it's a quick appeal to consistency to arrive at the
    conclusion that it's the preferred method overall.

    In summary, it appears to me that defining a method in the constructor
    gives you every advantage of prototyping the method, except one: you
    can prototype a method in without access to the constructor code. On
    the other hand, prototyping has the bigger disadvantage of not being
    able to use the forementioned closure. Well, at least *I* think it's a
    bigger disadvantage. Plus I like the "containedness" of defining
    functions within the constructor, vs. the sprawl of prototyping.

    So what am I missing?

    Lance
    cainlevy, Apr 20, 2005
    #1
    1. Advertising

  2. cainlevy wrote:

    > What are the pros and cons of defining methods in the constructor vs
    > through the prototype?


    I've answered this before:
    <URL:http://groups.google.co.uk/groups?selm=EJV1e.7128%24Ab.2871%40text.news.blueyonder.co.uk>

    [snip]

    > Currently, I'm of the mindset that [...] method [definition during
    > instantiation] is preferable in all situations [...]


    I would disagree. If you don't need to create a closure, or
    unnecessarily priviledge a method, then don't.

    [snip]

    > Part of my reasoning is personal preference for how the code looks


    Whilst aesthetically pleasing code is nice, I don't think it's a reason
    for doing something where a better alternative exists.

    [snip]

    > function MyObj()
    > {
    > var self = this;
    > self.msg = 'Hello World';
    > self.toString = function() {return self.msg};
    > }


    This is a very bad example. No closure is necessary as the following
    code is precisely the same.

    function MyObj() {
    this.msg = 'Hello World';
    }
    MyObj.prototype.toString = function() {return this.msg;};

    A better example would have been:

    function MyObj() {
    var msg = 'Hello World';

    this.toString = function() {return msg;};
    }

    where a closure /is/ necessary.

    [snip]

    > Plus I like the "containedness" of defining functions within the
    > constructor, vs. the sprawl of prototyping.


    I fail to see how

    function MyObj() {
    }
    MyObj.method = function() {/* ... */};

    could be considered sprawling when compared to

    function MyObj() {
    this.method = function() {/* ... */};
    }

    However, I would consider excessive method definition within the
    constructor to be poor form as you'd start to obscure the important
    details of instantiation.

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.
    Michael Winter, Apr 20, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Allison
    Replies:
    37
    Views:
    775
    Warren W. Gay VE3WWG
    Mar 4, 2004
  2. Ludovic Brenta
    Replies:
    86
    Views:
    1,347
    Jan C. =?iso-8859-1?Q?Vorbr=FCggen?=
    Feb 19, 2004
  3. rs
    Replies:
    13
    Views:
    492
    Mark Gordon
    Dec 1, 2003
  4. Dave Allison

    Announcing new scripting/prototyping language

    Dave Allison, Feb 5, 2004, in forum: C Programming
    Replies:
    35
    Views:
    861
    Warren W. Gay VE3WWG
    Mar 4, 2004
  5. Ludovic Brenta
    Replies:
    77
    Views:
    1,440
    Hyman Rosen
    Feb 26, 2004
Loading...

Share This Page