2013-06-19 160 views
0

嗨,我讓我的代碼。此一uncought引用異常內的對象是什麼,我有:訪問另一個對象

var config = { 
    debug: true, 
    data: { 
     debug: false, 
     logErrorsOnServer: true, 
     defaultCulture: '', 
     serviceUrl: '' 
    }, 

    init: function(options) { 
     if (!options) { 
      return; 
     }   
     if (options.hasOwnProperty('debug')) { 
      data.debug = options.debug; 
     } 

    }, 
}; 

當我試圖讓data.debug的價值,我得到一個uncought參考錯誤,說:

UncoughtReference Error: data is not defined 

我不能訪問我的數據對象嗎?

+1

可能是錯誤的,但嘗試this.data.debug –

+0

什麼問題。乍一看在Chrome devtools中一切正常 –

+0

data.debug = options.debug拋出UncoughtReference錯誤:數據未定義 – aleczandru

回答

1

你需要說:

this.data.debug = options.debug; 

...假設你是調用init()功能,設置this的(外)對象的方式,例如,與config.init()

或者你可以說:

config.data.debug = options.debug; 

你有原因有關data當您嘗試使用data.debug沒有被定義的錯誤,直接的是,其實data是定義爲一個變量,它是該對象的屬性。僅僅因爲init()是您的對象上的方法並不意味着它會自動引用其他對象的屬性。