开发者

Javascript: how to access property whose name is dynamic? [duplicate]

开发者 https://www.devze.com 2023-03-28 18:09 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Javascript create variable from its name
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Javascript create variable from its name

The code below checks to see if the javascript object form_errors has the property whose name is specified by this.name, where this refers to a text input

if (form_errors.hasOwnProperty(this.name)) {
  alert(form_errors.<this.name>;
}

How can I access the property without hard-coding the property name but leaving in the generalized form this.name ? Tha开发者_C百科nks.


Use brackets:

form_errors[this.name]

You can access any property of an object by passing in a string with its name. For instance, foo.bar and foo['bar'] have the same result.

0

精彩评论

暂无评论...
验证码 换一张
取 消