Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > constructing vs prototyping

Reply
Thread Tools

constructing vs prototyping

 
 
cainlevy
Guest
Posts: n/a
 
      04-20-2005
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

 
Reply With Quote
 
 
 
 
Michael Winter
Guest
Posts: n/a
 
      04-20-2005
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Announcing new scripting/prototyping language Dave Allison C Programming 35 03-04-2004 05:45 PM
Announcing new scripting/prototyping language Dave Allison C++ 37 03-04-2004 05:45 PM
Re: No call for Ada (was Re: Announcing new scripting/prototyping language) Ludovic Brenta C Programming 77 02-26-2004 02:36 PM
Re: No call for Ada (was Re: Announcing new scripting/prototyping language) Ludovic Brenta C++ 86 02-19-2004 09:19 AM
Prototyping rules/recommendations sought rs C Programming 13 12-01-2003 10:13 PM



Advertisments