473,385 Members | 1,673 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.

Object as a prototype.

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!

Jan 11 '07 #1
2 1076
jonny...@gmail.com wrote:
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.

Jan 11 '07 #2


On 11 Jan, 13:11, "Richard Cornford" <Rich...@litotes.demon.co.uk>
wrote:
jonny...@gmail.com wrote:
I wanted to add an object as a prototypeAs 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" issuesThere 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.
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.

Jan 11 '07 #3

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

Similar topics

7
by: Bennett Haselton | last post by:
Is there any way to find a string representing an object's class, which will work in Internet Explorer 6? "typeof" doesn't work -- it returns "object" for all objects: x =...
6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
2
by: Richard Trahan | last post by:
In the 1.5 Guide, Chapter 8 (Details of the Object Model), last sentence: "The dennis object does not inherit this new property". I don't understand why this is so. A few pages earlier, it says...
7
by: Florian Loitsch | last post by:
hi, in section 10.1.8 (Arguments Object) it is stated that the "internal ] property of the arguments object is the orginal Object prototype object, the one that is the *initial* value of...
4
by: Luke Matuszewski | last post by:
Here are some questions that i am interested about and wanted to here an explanation/discussion: 1. (general) Is the objectness in JavaScript was supported from the very first version of it (in...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
15
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
3
by: jacobstr | last post by:
I've noticed Object.extend used in a few different ways and I'm having trouble distinguishing why certain usages apply to a given situation. On line 804 Ajax.Base is defined as follows: ...
14
by: julie.siebel | last post by:
I've been wrestling with a really complex page. All the data is drawn down via SQL, the page is built via VBScript, and then controlled through javascript. It's a page for a travel company that...
23
Frinavale
by: Frinavale | last post by:
JavaScript is a very strange place for me... So, I decided that, before I attempt to create an Object, I should first learn about Objects. I had actually created one before with the help of a...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.