473,322 Members | 1,405 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,322 software developers and data experts.

Different ways to declare methods/properties of an object

Well, I've been working with JS for three years and have a great
experience here.
But! I still have no really acceptable answer to the following
question:
What is the principle difference between declaring methods/properties
in the constructor function body and via prototypes.

Are there any real GURUs? Let's discuss the issue.

P.S. Currently I am working on XUL implementation for IE, so there is
a lot of OOP code written in JS to be potentially optimized
http://www.magicofphoto.com/web/
Jul 23 '05 #1
2 2305


Sergey Ilinsky wrote:

What is the principle difference between declaring methods/properties
in the constructor function body and via prototypes.


There is one prototype object shared by the instances created with one
constructor function thus if you want a "class" of objects then it makes
sense to at least put all methods on the prototype as that is far more
efficient and far more meaningful, a method defines the dynamic
behaviour of an object and is usually expected to be consistent for all
instances in a "class". Thus methods should be created by adding a
function property to the prototype e.g.

function God (name) {
this.name = name;
}
God.prototype.praise = function () {
alert('All hail ' + this.name);
};

Now all instances e.g.
var god1 = new God('Kibo');
var god2 = new God('Xibo');
do not have their own function property named praise but share one via
the prototype and that is the proper way if all instances are supposed
to have the same behaviour.

And if you did

function God (name) {
this.name = name;
this.praise = function () {
alert('All hail ' + this.name);
}
}

then each instance created with new God('someName') would have its own
function property named praise but as the body is identical you would
just waste storage space then.

So having

function SomeConstructorFunction (args) {
...
}
SomeConstructorFunction.prototype.methodName = function (...) {
...
};

is just JavaScript's operational way to declare a common method for a
"class" of objects.
For normal non function properties it is different, usually in object
oriented development those properties make up the data of the object
instance which make it unique so there it usually doesn't make sense to
have a property on the prototype, unless for instance you wanted to have
a default value for a property.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
On 28/03/2005 10:49, Sergey Ilinsky wrote:
What is the principle difference between declaring methods/properties
in the constructor function body and via prototypes.


From a performance point of view, using the prototype object uses
less memory and allows for quicker object instantiation as the
properties are only created once, and only exist in one place. A
possible advantage to the use of constructor-defined properties is
that the property will exist on the object itself and will therefore
take less time to find during member look-up operations. That said, I
think it would take a long prototype chain to necessitate code like that.

As for behavioural differences, there is clearly no other way to
create private members, or privileged public members, than through
creation in the constructor during instantiation.

Mike
Hmmm, that seemed awfully rushed...

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Derrick | last post by:
How does one declare an event within an interface, so that every class which implements that interface must implement that event? I think I just need to specifiy the actual event as I would a...
5
by: Wysiwyg | last post by:
I'm new to c# programming and can't figure out how to avoid duplicating common code in multiple classes when I'm restricted to using different system base classes.. I'm using c# in asp.net to write...
5
by: Juan T. Llibre | last post by:
OK, guys, usually I answer questions instead of asking them, but this thing has me scratching my head. Why is the default for AutoEventWireup different for C# and VB.NET ? In VS 2005, if I...
6
by: rtilley | last post by:
s = ' qazwsx ' # How are these different? print s.strip() print str.strip(s) Do string objects all have the attribute strip()? If so, why is str.strip() needed? Really, I'm just curious......
12
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
17
by: romixnews | last post by:
Hi, I'm facing the problem of analyzing a memory allocation dynamic and object creation dynamics of a very big C++ application with a goal of optimizing its performance and eventually also...
26
by: Cliff Williams | last post by:
Can someone explain the pros/cons of these different ways of creating a class? // 1 function myclass() { this.foo1 = function() {...} } // 2a
0
ADezii
by: ADezii | last post by:
The motivation for this Tip was a question asked by one of our Resident Experts, FishVal. The question was: How to Declare Default Method/Property in a Class Module? My response to the question was...
4
by: Karol Kowcik | last post by:
Here's an example of 3 ways of using a method of an object with a different object context. Is there a signficant difference between method 1., where a reference to beta's method is copied and 2.,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.