这个问题可能有一个快速的答案,但我无法弄清楚我做错了什么。我有一个类EmailList:
var EmailList = function(id, name, expire_date)
{
var instance = this;
var list_id = id;
var list_name = name;
var expire_date = expire_date;
var is_editing = false;
this.get_list_id = function() { return instance.list_id; }
this.get_list_name = function() { return instance.list_name; }
this.get_expire_date = function() { return instance.expire_date; }
this.is_editing = function() { return instance.is_editing; }
}
当我调用类中的任何方法时,我感到困惑,例如:
console.log(list.get_list_name());
console.log(list.get_list_id());
结果我得到'未定义'。这是怎么回事?
答案 0 :(得分:4)
您没有初始化实例数据。而不是声明局部变量来存储参数,而是尝试这样做:
var EmailList = function(id, name, expire_date)
{
var instance = this;
instance.list_id = id;
instance.list_name = name;
instance.expire_date = expire_date;
instance.is_editing = false;
this.get_list_id = function() { return instance.list_id; }
this.get_list_name = function() { return instance.list_name; }
this.get_expire_date = function() { return instance.expire_date; }
this.is_editing = function() { return instance.is_editing; }
}
顺便说一下,更好的方法是消除instance
变量,更改实例方法以使用this
并使用原型来定义方法:
var EmailList = function(id, name, expire_date)
{
this.list_id = id;
this.list_name = name;
this.expire_date = expire_date;
this.is_editing = false;
};
EmailList.prototype = {
get_list_id : function() { return this.list_id; },
get_list_name : function() { return this.list_name; },
get_expire_date : function() { return this.expire_date; },
is_editing : function() { return this.is_editing; }
};
这样您就不会为EmailList
的每个实例创建单独的成员函数对象。
答案 1 :(得分:1)
Ted Hopp的答案中的两个示例都将使您的代码正常工作,但看起来您可能正在尝试为私有字段(或等效于私有字段的JavaScript)创建访问器。如果是这样,你最好这样做:
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());
}
}
或者你可以这样做,并且完全跳过三个额外的变量声明:
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());
}
}
这样可以确保无法从对象外修改list_id
,list_name
,expire_date
和is_editing
。
EcmaScript 5中提供的另一种替代方法是创建真正的只读属性,任何人都无法修改:
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());
}
}
我认识到让is_editing
完全只读是可能没有意义,但希望这会给你一些好主意。