Different ways to declare methods/properties of an object

S

Sergey Ilinsky

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/
 
M

Martin Honnen

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.
 
M

Michael Winter

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top