2014-09-12 58 views
0

我試圖動態訪問一個包含在字符串中的屬性值的對象。示例如下:使用字符串變量的值作爲對象參數訪問器

var toolState = { 
    draw_point: false; 
    draw_line: false; 
} 

var dynamicText = "draw_point"; 

toolState.dynamicText = true; //here is the problem 

我對JS相當陌生。對不起,如果這是一個愚蠢的問題。

謝謝

+0

嘗試'toolState [dynamicText]'。 – blex 2014-09-12 15:33:13

+0

toolState [dynamicText]或toolState [「propertyName」] – 2014-09-12 15:33:27

+0

[動態訪問對象屬性使用變量](http://stackoverflow.com/q/4244896/218196),可能[更多](http:// stackoverflow。 com/search?q =%5Bjavascript%5D + dynamic + property + variable) – 2014-09-12 15:34:49

回答

1

使用括號符號而不是點符號表示變量名稱作爲屬性。

toolState[dynamicText] = true; 
相關問題