Object 对象
一、声明对象的方式
1、字面量声明
书写格式: 关键字 标识符 赋值符号 大括号 分号
let F70 = {} ;
let F70 = {};
console.log(F70,typeof F70); // {} object
// 对象的初始化{
// 属性名1:属性值1,
// 属性名2:属性值2,
// ...
// 属性名n:属性值n,
// 方法名1:function(){代码块},
// 方法名2:function(){代码块},
// ...
// 方法名n:function(){代码块}
// }
let zjk = {
eyes: 2,
legs: 2,
height: 170,
life: 100,
money: 1000,
changge: function () {
console.log('changge');
this.money += 500;
},
kandianying: function () {
console.log('kandianying');
this.money -= 50;
}
}
二、调用对象
1、当使用对象不存在的属性,会得到undefined,
原因:使用之前,Js会在对象中添加此属性,并赋值undefined。
let F70 ;
console.log(F70); // undefined
let F71 = [];
console.log(F71.zjk); // undefined
let F72 = {};
console.log(F72.name); // undefined
三、对象的增删查改
1、属性和方法的添加
let zjk = {
gf:'1',
};
zjk.money = 500;
console.log(zjk.money);
zjk.changge = function () {
console.log('changge');
this.money += 500;
};
console.log(zjk); // { gf: '1', money: 500, changge: [Function] }
zjk.changge(); // changge
console.log(zjk.money);//1000
添加一个存在的属性,会把原有的升序进行覆盖,
为了解决这个问题,会声明一个Symbol类型的变量。
声明Symbol变量
注:1、小括号里的内容时Symbol变量的注释
2、声明时不要加new
let money = Symbol();
给Symbol变量赋值(需要告知给哪个对象添加)
对象名[Symbol变量] = 属性值;
zjk[money] = 0 ;
使用方式:属性名[Symbol变量]
let zjk = {
gf:'1',
money:100
};
zjk[money] = 0 ;
console.log(zjk[money]);// 0
2、属性和方法的修改
let zjk = {
gf:'1',
money:100
};
zjk.money = 10000;
console.log(zjk.money); // 10000
3、属性和方法的删除
删除属性的格式:delete对象名.属性; 使用被删除的属性,会得到undefined,
let zjk = {
gf:'1',
money:500,
changge:function () {
console.log('changge');
this.money += 500;
}
};
console.log(zjk); // { gf: '1', money: 500, changge: [Function] }
delete zjk.gf; // { money: 500, changge: [Function] }
console.log(zjk.gf); // undefined
删除方法的格式:delete对象名.方法名;
注:方法名后面不要加小括号
调用删除的方法,会报错(对象没有这个方法)
let zjk = {
gf:'1',
money:500,
changge:function () {
console.log('changge');
this.money += 500;
}
};
delete zjk.changge; // console.log(zjk); // { gf: '1', money: 500 }
console.log(zjk.changge()); // zjk.changge is not a function
4、属性和方法的查找
let F70 = [
{ name: "F70", pwd: "123a", balance: 1000 },
{ pwd: "123b", balance: 2000, name: "F71" },
{ name: "F72", pwd: "123c", balance: 3000 },
];
console.log(F70[1].name); //F71
console.log(F70[1]["name"]);//F71
let a = "balance";
console.log(F70[1][a]); //[2000,Function: song]
四、遍历对象
let zjk = {
name:'ZJK',
age:18,
gender:'male'
}
1、遍历对象的键名
for (const item of Object.keys(zjk)) {
console.log(`键名:${item}`);
}
// 键名:name 键名:age 键名:gender
2、遍历对象的键值
for (const item of Object.values(zjk)) {
console.log(`键值:${item}`);
}
// 键值:ZJK 键值:18 键值:male
3、遍历键对象的值对
for (const item of Object.entries(zjk)) {
console.log(`键值对:${item}`);
}
// 键值对:name,ZJK 键值对:age,18 键值对:gender,male
4、判断对象中是否存在要查找的键名
console.log("name"in zjk); // true
console.log('123' in zjk); // false
console.log(zjk['name']); // ZJK
五、对象的嵌套
let wulinmeng = {
name: "WLM",
age: "18",
gender: "male",
gf: {
name: "xiaohua1",
age: 18,
song: function() {
console.log(`酒醉的蝴蝶!!!`);
},
dance: function() {
console.log(`动词打次!!!`);
}
}
};
console.log(wulinmeng.gf.song); // [Function: song]
内置对象
string 的属性
let str = 'a-b-c';
console.log(str.length); // 5
console.log(str); // a-b-c
string 的方法
1、字符串转数组
let str = 'a-b-c';
console.log(str.split("-")); // [ 'a', 'b', 'c' ]
2、去除字符串前后(左右)的空格
let str = ' 123 ';
console.log(str.length); // 11
//去除字符串左侧空格
str = str.trimLeft();
console.log(str.length); // 7
//去除字符串右侧空格
str = str.trimRight();
console.log(str.length); // 3
//去除字符串左右的空格
str = ' 123 ';
console.log(str.length); // 11
str = str.trim()
console.log(str.length); // 3
3、获取字符串内容
let str = 'hello F70';
console.log(str.slice(1,-2)); // ello F
substring 如果存在负数,会转换成 0
console.log(str.substring(1,-2)); // h
console.log(str.substring(3,-2)); // hel
console.log(str.slice(1,3)); // el
console.log(str.substring(1,3 )); // el
4、字符串转大小写
let str = "zjk";
console.log(str.repeat(5)); // zjkzjkzjkzjkzjk
// 字符串转大写 str.toUpperCase()
console.log(str.toUpperCase()); // ZJK
// 字符串转小写 str.toLowerCase()
console.log(str.toLowerCase()); // zjk
5、判断是以XXX开始或者结束
判断是以XXX开始 str.startsWith(‘zj’)
let str = 'zjk'
console.log(str.startsWith('zj')); // true
console.log(str.startsWith('j')); // false
判断是以XXX结束 str.endsWith(“jk”)
let str = 'zjk'
console.log(str.endsWith("jk")); // true
console.log(str.endsWith("j")); // false
Date 日期
注:Data 数据 Date 日期
let date1 = new Date();
console.log(date1); // 打印当前时间 2020-09-18T07:16:41.709Z
console.log(date1.getFullYear()); // 年 getFullYear
console.log(date1.getMonth()); // 月 0 ——11 用的时候+1 getMonth
console.log(date1.getDate()); // 日 getDate
console.log(date1.getHours()); // 时 getHours
console.log(date1.getMinutes()); // 分 getMinutes
console.log(date1.getSeconds()); // 秒 getSeconds