object對象數(shù)據(jù)類型-普通對象
{[key]: [value]}任何一個對象都是由0到多個鍵值對(屬性名: 屬性值)組成的(并且屬性名不能重復(fù))
let person = {
name: '張三',
age: 40,
height: '185cm',
weight: '75kg',
1: 100
};
// 刪除屬性
// => 真刪除:把屬性徹底干掉
delete person[1];
console.log(person); // => null
// => 假刪除:屬性還在,值為空
person.weight = null;
console.log(person); // => person.weight => null
// 設(shè)置屬性名屬性值
person.GF = '猿'; // 設(shè)置新的屬性名、屬性值
person.name = '李四'; // 屬性名不能重復(fù),如果屬性名已存在,不屬于新增屬性值而是屬于修改屬性值
// 獲取屬性名對應(yīng)的屬性值
// 方式一: 對象.屬性名
console.log(person.name);
// 方式二: 對象[屬性名],屬性名是數(shù)字或者字符串格式的
console.log(person['age']);
// 如果當(dāng)前屬性名不存在,默認(rèn)的屬性值是undefined
console.log(person.sex); // => undefined
// 如果屬性名是數(shù)字,則不能使用點的方式獲取屬性值
console.log(person[1]); // => 100
console.log(person.1); // => SyntaxError(語法錯誤)
數(shù)組是特殊的對象數(shù)據(jù)類型
- 我們中括號中設(shè)置的是屬性值,它的屬性名是默認(rèn)生成的數(shù)字,從零開始遞增,而且這個數(shù)字代表每一項的位置,我們把其稱為“索引” => 從零開始,連續(xù)遞增,代表每一項位置的數(shù)字屬性名
- 天生默認(rèn)一個屬性名:length,存儲數(shù)組的長度
let arr = [12, '哈哈', true, 13];
console.log(arr);
// => [
// 0: 12,
// 1: '哈哈',
// 2: true,
// 3: 13,
// length: 4
//]
// 獲取數(shù)組中元素的個數(shù)
console.log(arr['length']); // => 4
console.log(arr.length); // => 4
console.log(arr[1]); // => '哈哈'
// 向數(shù)組中最后一項添加內(nèi)容
arr[arr.length] = 100;
arr.push(100);