Object as a prototype.

J

jonnys5k

I wanted to add an object as a prototype to separate my methods more
nicely, however, I ran into a couple of problems. Apart from the
obvious "scope" issues I found that any instances of my class shared
the objects methods and properties.

I realise (now) that this is actually how prototypes work, they share
functions and objects rather than create new instances of them for
every "class", but is there any way around it? (or shouldn't I be doing
things like this at all?)

Example:

var class = function()
{
[...]
}
class.prototype.method = function()
{
[...]
}
class.prototype.object = {

randomNumber : Math.random()*255,

method : function() {
{
return this.randomNumber;
}

}

var blah1 = new class();
var blah2 = new class();

blah1.object.method() // returns 42
blah2.object.method() // returns 42, the same!
 
R

Richard Cornford

I wanted to add an object as a prototype

As a prototype or to a prototype?
to separate my methods more nicely,

How does "more nicely" follow from that?
however, I ran into a couple of problems. Apart from the
obvious "scope" issues

There are no scope issues in the code posted here. (There are no local
variables, formal parameters or inner function declarations)
I found that any instances of my class shared
the objects methods and properties.

If you mean the object assigned as - class.prototype.object - then all
instances of - class - (objects created with the - class - constructor)
share a reference to that one object, and it only has one set of its
methods.
I realise (now) that this is actually how prototypes work,

And it is very useful to be able to default instance methods to a
single function object instance (as it avoids making many instances of
function objects that are essentially the same).
they share functions and objects rather than create new instances
of them for every "class", but is there any way around it? (or shouldn't
I be doing things like this at all?)
<snip>

A way around what precisely? You can create unique function object
instances to act as the methods of constructed object instances, but
you should not want to do that unless the unique identity of those
function objects is providing some useful facility.

Code design should follow from what the code is supposed to achieve/do.
Vague criteria of "more nicely" are not sufficient to dictate design
criteria.

Richard.
 
J

jonnys5k

variables, formal parameters or inner function declarations)

instances of - class - (objects created with the - class - constructor)
share a reference to that one object, and it only has one set of its
methods.

single function object instance (as it avoids making many instances of
function objects that are essentially the same).


A way around what precisely? You can create unique function object
instances to act as the methods of constructed object instances, but
you should not want to do that unless the unique identity of those
function objects is providing some useful facility.

Code design should follow from what the code is supposed to achieve/do.
Vague criteria of "more nicely" are not sufficient to dictate design
criteria.

Richard.

I wanted to group methods into separate objects inside of the main
"class" only for aesthetic reasons, so I could have something like
this:

klass.events.add()
klass.stylesheets.add()

...etc.
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top