向已有对象添加新属性

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4.  
  5. <h1>JavaScript 对象属性</h1>
  6.  
  7. <p>您可以向现有对象添加新属性。</p>
  8.  
  9. <p id="demo"></p>
  10.  
  11. <script>
  12. var person = {
  13. firstname:"Bill",
  14. lastname:"Gates",
  15. age:62,
  16. eyecolor:"blue"
  17. };
  18.  
  19. person.nationality = "English";
  20. document.getElementById("demo").innerHTML =
  21. person.firstname + " is " + person.nationality + ".";
  22. </script>
  23.  
  24. </body>
  25. </html>