有两种方法可以修改嵌套对象的属性。一种是点方法,另一种是括号方法。两种方法的功能 相同,但唯一的区别是它们的符号。
让我们详细讨论一下。
在下面的示例中,最初,财产国家的值是英格兰。但是使用 点表示法会将值更改为印度。
<html> <body> <script> var person; var txt = ''; person = { "name":"Ram", "age":27, "address": { "houseno":123, "streetname":"Baker street", "country":"England" } } document.write("Before change : " + " " + person.address.country); person.address.country = "India"; document.write("</br>"); document.write("After change : " + " " + person.address.country); </script> </body> </html>
Before change : England After change : India
在以下示例中,使用方 括号表示法将属性“国家”的值从英国更改为印度。
<html> <body> <script> var person; var txt = ''; person = { "name":"Ram", "age":27, "address": { "houseno":123, "streetname":"Baker street", "country":"England" } } document.write("Before change : " + " " + person.address["country"]); person.address.country = "India"; document.write("</br>"); document.write("After change : " + " " + person.address["country"]); </script> </body> </html>
Before change : England After change : India