Connecting Tech Pros Worldwide Help | Site Map

having more than one object without using prototype

  #1  
Old January 23rd, 2007, 06:05 PM
James Black
Guest
 
Posts: n/a
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.

  #2  
Old January 23rd, 2007, 06:45 PM
VK
Guest
 
Posts: n/a

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();

  #3  
Old January 23rd, 2007, 06:55 PM
VK
Guest
 
Posts: n/a

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.

  #4  
Old January 23rd, 2007, 07:35 PM
Oliven
Guest
 
Posts: n/a

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();

  #5  
Old January 23rd, 2007, 08:15 PM
James Black
Guest
 
Posts: n/a

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.

  #6  
Old January 23rd, 2007, 08:15 PM
James Black
Guest
 
Posts: n/a

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.

  #7  
Old January 23rd, 2007, 08:35 PM
James Black
Guest
 
Posts: n/a

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.

  #8  
Old January 23rd, 2007, 08:35 PM
James Black
Guest
 
Posts: n/a

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.

  #9  
Old January 24th, 2007, 12:05 AM
RobG
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
to learn jQuery if already using prototype liketofindoutwhy answers 83 June 27th, 2008 08:10 PM
Advantage of using prototype objects over regular functions? Yansky answers 7 February 12th, 2008 07:45 PM
setTimeout and an object's methods Andrew Poulos answers 12 March 13th, 2006 11:55 PM
Protected static members, abstract classes, object composition vs. subclassing Kevin Prichard answers 11 November 29th, 2005 01:45 AM