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