473,385 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1789
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: Dave Allison | last post by:
Oh no, not another "check out my cool new language" posting :-) For about 5 years now, I have been developing a scripting/prototyping language that is now available on the net. It's called...
9
by: Carl | last post by:
I have been using Python for quite some time now and I love it. I use it mainly for explorative computing and numerical prototyping, ie testing and trying out different kinds of algorithms and...
1
by: Will | last post by:
function obj1() { this.children; this.Children = function() { if(typeof(this.children) === 'undefined') { this.children = new col(); } return this.children; }
4
by: Jeff Thies | last post by:
It seems to me that document.getElementById and document.all are essentially the same thing. I'm thinking of prototyping document.getElementById for those browsers that understand document.all...
13
by: rs | last post by:
Dear All, I have a question regarding proptypes for functions. What is the recommended practice? The way I do it is to put all my external functions in a header file, while protyping...
4
by: Tilted | last post by:
Does anyone here use prototyping tools? I'm building one myself as I feel like I'm doing the same thing over and over again with the majority of my projects, how do people generally feel about...
1
by: matt.rasmus | last post by:
I have been using python for the last two years to create various visualizations for my research in computational biology. Over the years, I found that I often needed the same kinds of features...
2
by: ChrisO | last post by:
I've been pretty infatuated with JSON for some time now since "discovering" it a while back. (It's been there all along in JavaScript, but it was just never "noticed" or used by most until...
1
by: kardon33 | last post by:
Ok I have a problem when i try to install a perl extension that was a converted C header file using h2xs. My problem starts after i run "perl Makefil.PL" when i try to run "nmake". I get the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.