By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,776 Members | 1,400 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,776 IT Pros & Developers. It's quick & easy.

Different ways to declare methods/properties of an object

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a


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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.