[ Day 5 ] - 物件

物件

物件的格式

  • 会使用 { } 前後包住资料集
  • 内容会有属性和值
let shop = {
Name : "happy Store",
status : "Open",
storeItems : 5
};

如何读取物件内的格式

  • 读取的方式是利用「.」
let shop = {
Name : "happy Store",
status : "Open",
storeItems : 5
};


console.log(shop.status);  // 结果为 Open
  • 读取的方式也可以懽过 [ ] (中括号)来存取
    在遇到属性值是数字开头其他特别的字串时,就可以用此方式进行资料读取
let shop = {
Name : "happy Store",
status : "Open",
storeItems : 5,
"999" : "HI"
};

a = 'Name';
console.log(shop[a]);  // 结果为 happy Store
console.log(shop['Name']); // 结果为 happy Store
console.log(shop['999']); // 结果为 HI

新增与修改属性资料

  • 使用 = 赋予资料值
let shop = {};
shop.name = 'haapy Store';


console.log(shop.name); // 结果为 happy Store
  • 修改属性资料值一样是使用 = 赋予资料
let shop = {
Name : "happy Store",
status : "Open",
storeItems : 5
};

shop.name = 'happy happy';
console.log(shop.name); //  结果为 happly happy

删除属性

使用 delete 来进行删除

let shop = {
Name : "happy Store",
status : "Open",
storeItems : 5
};


delete shop.storeItems;
console.log(shop.storeItems); //  结果变成 undefined

後记

今天是第五天,说明了物件的格式及新增修改及读取的方式
这次我的学习纪录,欢迎各路大神的指点指教
我们下次见~


<<:  [Day5] 自我必备驱动力:以终为始

>>:  Day 19 - Execution Context

夜间模式真的对眼睛比较好吗? 详细整理(中)

上篇讨论了 蓝光 与 睡眠影响,这篇接着讨论其他争论点 3.眼睛疲劳 这个世代,数位使用普及化与时间...

Day04:Swift 基础语法— Swift Structure

Structure 假设我们有两个 function 如下: func myFunctionA ()...

D02 / 怎麽看到我在写什麽? - @Preview

今天大概会聊到的范围 @Preview annotation 及相关用法 原先的 xml 的 la...

Trouble with Distributed Systems (4-1) - Truth and Lies

前几天讲了跟分散式系统有关的网路不可靠、时钟不可靠的鬼故事,不可靠的东西这麽多,我们要如何判断真与假...

Day 3 Ruby 基础运算子

写在前面 刚开始学程序语言的时候总会有一些看起来很简单但很容易跳进去的坑,基础运算子还有逻辑运算在我...