2013-05-30 95 views
2

我有以下的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函數調用的數據對象包含所有數據 - 它似乎只是局部變量的賦值不起作用?我知道我一定在代碼中犯了一些明顯的錯誤。任何人都可以幫我找到我的問題嗎?

+1

啊,我的眼睛爲什麼要宣佈你的變數? – Ryan

+0

@ryan對不起,如果我打破你的眼睛: - )...請參閱「揭示模塊模式」:http://blog.pluralsight.com/2012/10/02/revealing-module-pattern-structuring-javascript-代碼部分iii/ –

+0

嗯,感謝您的鏈接,我以前沒有見過。我認爲我的本能反應不是這樣的,因爲JavaScript中的變量可以不使用'var'來聲明,但當然它最終會在全局範圍內展開,並且一看就是它正在發生的事情。 – Ryan

回答

3

您對本地變量的分配可能正在工作,但它們只是:分配給本地變量。您的代碼不會更新對象的屬性。事實上,通過return聲明來創建對象,該聲明從局部變量初始化屬性並不會創建局部變量和屬性之間的魔術鏈接。

你「getFromData」功能應該是這個樣子:

function getFromData(data) { 
    this.ID = data.ID; 
    this.AccountID = data.AccountID; 
    // and so on 
} 

集合屬性(「類」,「組」,「權限」)的工作,因爲他們的對象,並更新其內容。那部分沒關係。真的,你根本不需要這些局部變量;你可以直接初始化對象屬性。

+0

謝謝。所以過去常常看到我的大腦正在努力的其他語言重新回到了Javascript框架中。 –