17_对象.html 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. // 对象中也是存储多个值
  11. // 对象中内容的形式 是 键值对. key:value 每一对后面以逗号结束
  12. var person = {
  13. name:"张三",
  14. age:"18"
  15. }
  16. var goods = {
  17. img:"./img/a.png",
  18. title:"xiaomi",
  19. desc:"xxxxx"
  20. }
  21. // 对象中可以放置任何一种类型
  22. var video = {
  23. title:"xxxxx",
  24. like:100,
  25. show:true,
  26. talk:["xxxx","xxxx","xxx"]
  27. }
  28. // 如果对象中出现函数 那么称为对象方法
  29. // var person2 = {
  30. // name:"李四",
  31. // age:18,
  32. // say:function(){
  33. // console.log("hello");
  34. // }
  35. // }
  36. // 如何调用对象中的值 和 方法
  37. // 在对象中调用属性或方法 使用"."链接符号
  38. var person2 = {
  39. name:"李四",
  40. age:18,
  41. say:function(){
  42. console.log("hello");
  43. }
  44. }
  45. console.log(person2.name);
  46. person2.say();
  47. // 修改对象属性
  48. person2.name = "王五";
  49. // 输出对象属性
  50. delete person2.age;
  51. // 添加对象属性或方法
  52. person2.school = "xxx学校";
  53. console.log(person2)
  54. </script>
  55. </body>
  56. </html>