我有以下的Javascript對象定義:不能填充JavaScript對象屬性值與使用功能
var APIUserItem = function() {
var
id = '',
account_id = '',
client_id = '',
user_name = '',
salutation = '',
first_name = '',
middle_name = '',
last_name = '',
organization_name = '',
alternate_email = '',
time_zone_id = '',
utcoffset = '',
date_created = new Date(),
last_updated_date = new Date(),
is_active = null,
is_approved = null,
classes = [],
groups = [],
permissions = [],
properties = {},
version_stamp_hash_string = '',
getFromData = function (data) {
id = data.ID;
account_id = data.AccountID;
client_id = data.ClientID;
user_name = data.UserName;
salutation = data.Salutation;
first_name = data.FirstName;
middle_name = data.MiddleName;
last_name = data.LastName;
organization_name = data.OrganizationName;
alternate_email = data.AlternateEmail;
time_zone_id = data.TimeZoneID;
utcoffset = data.UTCOffset;
date_created = data.DateCreated;
last_updated_date = data.LastUpdatedDate;
is_active = data.IsActive;
is_approved = data.IsApproved;
properties = data.Properties;
version_stamp_hash_string = data.VersionStampHashString;
// list of pointers to classes
$.each(data.Classes, function (index, value) {
class_pointer = new APIPointerItem();
class_pointer.ID = value.ID;
class_pointer.PublicID = value.PublicID;
class_pointer.Name = value.Name;
class_pointer.RelativeURI = value.RelativeURI;
classes.push(class_pointer);
});
// list of pointers to Groups
$.each(data.Groups, function (index, value) {
group_pointer = new APIPointerItem();
group_pointer.ID = value.ID;
group_pointer.PublicID = value.PublicID;
group_pointer.Name = value.Name;
group_pointer.RelativeURI = value.RelativeURI;
groups.push(group_pointer);
});
// list of permissions
$.each(data.Permissions, function (index, value) {
permission_pointer = new APIPermissionList();
permission_pointer.ID = value.ID;
permission_pointer.Description = value.Description;
permission_pointer.Category = value.Category;
permission_pointer.Level = value.Level;
permission_pointer.ResourceType = value.ResourceType,
permission_pointer.Action = value.Action;
permissions.push(permission_pointer);
});
};
return {
ID: id,
AccountID: account_id,
ClientID: client_id,
UserName: user_name,
Salutation: salutation,
FirstName: first_name,
MiddleName: middle_name,
LastName: last_name,
OrganizationName: organization_name,
AlternateEmail: alternate_email,
TimeZoneID: time_zone_id,
UTCOffset: utcoffset,
DateCreated: date_created,
LastUpdatedDate: last_updated_date,
IsActive: is_active,
IsApproved: is_approved,
Classes: classes,
Groups: groups,
Permissions: permissions,
Properties: properties,
VersionStampHashString: version_stamp_hash_string,
GetFromData: getFromData
};
};
var user = new APIUserItem();
user.GetFromData(data);
然後嘗試訪問在新項目中的值,像這樣:
document.write(user.ID);
所有屬性值的回來空的,除了
當我通過調用new了一個APIUserItem對於包含預期數據的集合。例如,我可以從對象外部遍歷Groups數組,並訪問每個組的屬性以顯示值。
我傳遞給GetFromData函數調用的數據對象包含所有數據 - 它似乎只是局部變量的賦值不起作用?我知道我一定在代碼中犯了一些明顯的錯誤。任何人都可以幫我找到我的問題嗎?
啊,我的眼睛爲什麼要宣佈你的變數? – Ryan
@ryan對不起,如果我打破你的眼睛: - )...請參閱「揭示模塊模式」:http://blog.pluralsight.com/2012/10/02/revealing-module-pattern-structuring-javascript-代碼部分iii/ –
嗯,感謝您的鏈接,我以前沒有見過。我認爲我的本能反應不是這樣的,因爲JavaScript中的變量可以不使用'var'來聲明,但當然它最終會在全局範圍內展開,並且一看就是它正在發生的事情。 – Ryan