可以通过两种方式访问对象属性。一个是.property,另一个是[property]。
Object.property;
Object["property"];
为了更好地理解,让我们看下面的示例。
在下面的示例中,定义了一个名为“ person”的对象,并以点符号访问其属性。
<html> <body> <script> var person = { firstname:"Ram", lastname:"kumar", age:50, designation:"content developer" }; document.write(person.firstname + " " + "is in a role of" + " " + person.designation); </script> </body> </html>
输出结果
Ram is in a role of content developer
在下面的示例中,对象“人”的属性以 括号表示法访问。
<html> <body> <script> var person = { firstname:"Ram", lastname:"kumar", age:50, designation:"content developer" }; document.write(person['firstname']+ " " + "is in a role of" + " " + person['designation']); </script> </body> </html>
输出结果
Ram is in a role of content developer