JavaScript 中物件是什麼以及如何訪問任意屬性?**


訪問物件的屬性

如果沒有物件,就沒有 JavaScript。每一項內容(例如布林值、數字、函式等)都是物件。按照以下方法對訪問物件的屬性進行了解釋。

示例 1

 即時演示

<html>
<body>
<script>
   myObj = {"name":"Ramesh","age":30,"family":
           [{"mother":"Geetha","father":"Rao"}],"city":"Berlin"};
           var res = myObj.family[0].mother;
  document.write(res);
</script>
</body>
</html>

輸出

Geetha

示例 2

 即時演示

<html>
<body>
<script>
   myObj = {"name":"Ramesh","age":30,"family":
           [{"mother":"Geetha","father":"Rao"}],"city":"Berlin"};
   var res = myObj.city;
   document.write(res);
</script>
</body>
</html>

輸出

Berlin

更新於: 2019 年 7 月 30 日

已瀏覽 93 次

開啟你的 職業生涯

獲得課程認證

開始學習
廣告