having more than one object without using prototype

J

James Black

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

VK

James said:
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();
 
V

VK

VK said:
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.
 
O

Oliven

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

James Black

VK said:
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.
 
J

James Black

Oliven said:
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.
 
J

James Black

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

James Black

James said:
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.
 
R

RobG

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_info/private_static.html >

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

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,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top