471,338 Members | 1,301 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

constructing vs prototyping

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

Jul 23 '05 #1
1 1678
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.bluey onder.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.
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

30 posts views Thread by Dave Allison | last post: by
9 posts views Thread by Carl | last post: by
1 post views Thread by Will | last post: by
4 posts views Thread by Jeff Thies | last post: by
4 posts views Thread by Tilted | last post: by
1 post views Thread by matt.rasmus | last post: by
1 post views Thread by kardon33 | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.