Both examples in Ted Hopp's answer will get your code to work, but it looks like you may be trying to make accessors for private fields (or the JavaScript equivalent of private fields). If so, you may be better off doing this:
var EmailList = function(id, name, expire_date)
{
var list_id = id;
var list_name = name;
// Clone input date so it can't be modified externally
var expire_date = cloneDate(expireDate);
var is_editing = false;
this.get_list_id = function() { return list_id; }
this.get_list_name = function() { return list_name; }
// Clone date so it can't be modified externally
this.get_expire_date = function() { return cloneDate(expire_date); }
this.is_editing = function() { return is_editing; }
function cloneDate(date) {
return date && date.getTime && new Date(date.getTime());
}
}
Or you can do this, and skip three of the extra variable declarations altogether:
var EmailList = function(id, name, expire_date)
{
var is_editing = false;
// Clone input date so it can't get modified externally
expire_date = cloneDate(expire_date);
this.get_list_id = function() { return id; }
this.get_list_name = function() { return name; }
// Clone date so it can't get modified externally
this.get_expire_date = function() { return cloneDate(expire_date); }
this.is_editing = function() { return is_editing; }
function cloneDate(date) {
return date && date.getTime && new Date(date.getTime());
}
}
This will ensure that list_id, list_name, expire_date and is_editing cannot be modified from outside your objects.
Yet another alternative available in EcmaScript 5 is to create true readonly properties, that can't be modified by anyone:
var EmailList = function(id, name, expire_date) {
Object.defineProperties(this, {
list_id: { value: id, writable: false },
list_name: { value: name, writable: false },
expire_date: { value: cloneDate(expire_date), writable: false },
is_editing: { value: false, writable: false });
function cloneDate(date) {
return date && date.getTime && new Date(date.getTime());
}
}
I recognize that it probably doesn't make sense to make is_editing completely readonly, but hopefully this gives you some good ideas.