Connecting Tech Pros Worldwide Forums | Help | Site Map

having more than one object without using prototype

James Black
Guest
 
Posts: n/a
#1: Jan 23 '07
I am working on my own pop up calendar, mainly because the one I am
currently using crashes the Safari browser at times.

So, I want to verify that what I am doing will work, in that I want to
be able to have multiple calendars open at the same time, each
independent of the other.

So, I start it off with:
var Calendar = {
dateSelected: null,
topPos:null,
leftPos:null,

somefunction:function(e) {
...
}
};

If I create more than one calendar object, will they have their own
variables, in that the dateSelected, topPos and leftPos will be unique
to that instance?

Or, is there a better way to do this, that is cross-platform.

Thank you for your help.


VK
Guest
 
Posts: n/a
#2: Jan 23 '07

re: having more than one object without using prototype



James Black wrote:
Quote:
var Calendar = {
dateSelected: null,
topPos:null,
leftPos:null,
>
somefunction:function(e) {
...
}
};
>
If I create more than one calendar object, will they have their own
variables, in that the dateSelected, topPos and leftPos will be unique
to that instance?
>
Or, is there a better way to do this, that is cross-platform.
function Calendar(arg) {
this.name = arg;
this.dataSelected = null;
this.topPos = null;
this.leftPos = null;
}

Calendar.prototype.somefunction = function(e) {
alert(this.name);
}

var cal1 = new Calendar('First');
var cal2 = new Calendar('Second');

cal1.somefunction();
cal2.somefunction();

VK
Guest
 
Posts: n/a
#3: Jan 23 '07

re: having more than one object without using prototype



VK wrote:
Quote:
function Calendar(arg) {
this.name = arg;
this.dataSelected = null;
this.topPos = null;
this.leftPos = null;
}
>
Calendar.prototype.somefunction = function(e) {
alert(this.name);
}
>
var cal1 = new Calendar('First');
var cal2 = new Calendar('Second');
>
cal1.somefunction();
cal2.somefunction();
Oops... : "having more than one object without using prototype"

There are other ways, but what is the exact problem with prototype? I
know that Safari 1.x-2.x is a junk, but still they did not fall so low
do not support prototype property.

Oliven
Guest
 
Posts: n/a
#4: Jan 23 '07

re: having more than one object without using prototype




On 23 jan, 18:53, "James Black" <planiturth...@gmail.comwrote:
Quote:
I am working on my own pop up calendar, mainly because the one I am
currently using crashes the Safari browser at times.
>
So, I want to verify that what I am doing will work, in that I want to
be able to have multiple calendars open at the same time, each
independent of the other.
>
So, I start it off with:
var Calendar = {
dateSelected: null,
topPos:null,
leftPos:null,
>
somefunction:function(e) {
...
}
>
};If I create more than one calendar object, will they have their own
variables, in that the dateSelected, topPos and leftPos will be unique
to that instance?
>
Or, is there a better way to do this, that is cross-platform.
>
Thank you for your help.
function Calendar(arg) {
this.name = arg;
this.dataSelected = null;
this.topPos = null;
this.leftPos = null;
this.somefunction = calendar_somefunction;

}

function calendar_somefunction(e) {
alert(this.name);
}

var cal1 = new Calendar('First');
var cal2 = new Calendar('Second');

cal1.somefunction();
cal2.somefunction();

James Black
Guest
 
Posts: n/a
#5: Jan 23 '07

re: having more than one object without using prototype



VK wrote:
Quote:
>
Oops... : "having more than one object without using prototype"
>
There are other ways, but what is the exact problem with prototype? I
know that Safari 1.x-2.x is a junk, but still they did not fall so low
do not support prototype property.
Actually the problem is with prototype.js, or the prototype
framework, not with javascript prototypes.

Sorry, I was unclear in my first message.

James Black
Guest
 
Posts: n/a
#6: Jan 23 '07

re: having more than one object without using prototype



Oliven wrote:
Quote:
function Calendar(arg) {
this.name = arg;
this.dataSelected = null;
this.topPos = null;
this.leftPos = null;
this.somefunction = calendar_somefunction;
>
}
>
function calendar_somefunction(e) {
alert(this.name);
}
>
var cal1 = new Calendar('First');
var cal2 = new Calendar('Second');
>
cal1.somefunction();
cal2.somefunction();
Thank you to both of your for your responses.

I will probably not use the prototype way until I can feel more
comfortable with that, as it is one aspect of javascript that I have
limited experience with.

James Black
Guest
 
Posts: n/a
#7: Jan 23 '07

re: having more than one object without using prototype


I hope you two don't mind a question on how I would like to do this.

I am using namespaces, and so, is there any reason why this approach
would be bad?

var AcompCalendar = {
Calendar:function(arg) {
this.name = arg;
this.dateSelected = null;
this.topPos = null;
this.leftPos = null;
this.nextMonth = AcompCalendar.nextMonth;
};

nextMonth:function() {
alert('next month selected');
}
}


var cal1 = new AcompCalendar.Calendar('first one');
var cal2 = new AcompCalendar.Calendar('second one');


Thank you for your help.

James Black
Guest
 
Posts: n/a
#8: Jan 23 '07

re: having more than one object without using prototype



James Black wrote:
Quote:
I hope you two don't mind a question on how I would like to do this.
>
I am using namespaces, and so, is there any reason why this approach
would be bad?
>
var AcompCalendar = {
Calendar:function(arg) {
this.name = arg;
this.dateSelected = null;
this.topPos = null;
this.leftPos = null;
this.nextMonth = AcompCalendar.nextMonth;
},
>
nextMonth:function() {
alert('next month selected');
}
}
>
>
var cal1 = new AcompCalendar.Calendar('first one');
var cal2 = new AcompCalendar.Calendar('second one');
>
>
Thank you for your help.
I had a typo that I corrected here, I should have commas separating
functions not semi-colons.

RobG
Guest
 
Posts: n/a
#9: Jan 24 '07

re: having more than one object without using prototype




On Jan 24, 6:23 am, "James Black" <planiturth...@gmail.comwrote:
Quote:
I hope you two don't mind a question on how I would like to do this.
>
I am using namespaces, and so, is there any reason why this approach
would be bad?
>
var AcompCalendar = {
Calendar:function(arg) {
this.name = arg;
this.dateSelected = null;
this.topPos = null;
this.leftPos = null;
this.nextMonth = AcompCalendar.nextMonth;
};
>
nextMonth:function() {
alert('next month selected');
}
>
}var cal1 = new AcompCalendar.Calendar('first one');
var cal2 = new AcompCalendar.Calendar('second one');
"Bad" is likely too strong a word. The primary design flaws are:

1. It replicates methods and properties in every instance of the
calendar object that could be implemented only once in the calendar
object's prototype, therefore it is wasteful of resources.

2. If you change the name of the namespace (i.e. AcompCalendar), you
have to find and replace all instances of that namespace.

3. The (otherwise) unnecessary use of references to access methods,
e.g.

var AcompCalendar = {
Calendar : function(arg) {
...
this.nextMonth = AcompCalendar.nextMonth;
},
nextMonth : function() {
...
}
}

Of course there are pros and cons with other approaches too, but you
only asked for the cons of this approach. :-)

There are many different ways to implement this, what is best for you
is your decision. A namespaced version using the calendar object's
prototype might look like:

var AcompCalendar = {}

AcompCalendar.calendar = function(arg) {
this.name = arg;
this.dateSelected = null;
this.topPos = null;
this.leftPos = null;
}

AcompCalendar.calendar.prototype.nextMonth = function() {
alert(this.name + ': next month selected');
}

var cal1 = new AcompCalendar.calendar('first one');
var cal2 = new AcompCalendar.calendar('second one');

cal1.nextMonth()
cal2.nextMonth()

You might also like to read Richard Cornford's article:

<URL: http://www.litotes.demon.co.uk/js_in...te_static.html >

Take your time, there's lots to learn but once learned, it's very
valuable.


--
Rob

Closed Thread