• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
LilyLiya
博客园    首页    新随笔    联系   管理    订阅  订阅
JS objects
JavaScript Object Literals
  • objects.      
    • objects are collections of properties
    • properties are a key-value pair
    • use costom keys to access data rather than using an index.
  • key-value pairs:   property = key + value
    • username: "smart_rich"
    • upvotes: 7
    • text: "nice vlog!"
  • dictionay property. (一一对应)
  • all types welcome.(可以多种类型混合存储)
  • valid keys, all keys are converted to strings * (except for symbols)
  • access data out of objects
    • const papers = {
          red : "shdkshdks"
          blue: "jshdjshdd"
          yellow: "sdhjshd"
      }
      
      papers.red
      papers['blue']
      
      let color = 'red';
      papers[color] // "shdkshdks"

       

  • updating & adding properties
  • // To make an object literal:
    const dog = {
        name: "Rusty",
        breed: "unknown",
        isAlive: false,
        age: 7
    }
    // All keys will be turned into strings!
    
    // To retrieve a value:
    dog.age; //7
    dog["age"]; //7
    
    //updating values
    dog.breed = "mutt";
    dog["age"] = 8;

    //addin a new property
    dog["isHappy"] = true;

    Arrays + Objects

shoppingCart[2].product = "Fire Stick"

posted on 2021-01-09 03:05  LilyLiya  阅读(61)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3