对象(object)是js里的一种数据类型一种无序的数据集合
声明
let 对象名 { 属性名:属性值 方法名:函数}
属性访问
- 声明对象,并添加了若干属性后,可以使用 . 或 [] 获得对象中属性对应的值,我称之为属性访问。 简单理解就是获得对象里面的属性值
-
-
- 使用[ ]进行访问,变量名要加引号
对象中的方法访问
- 声明对象,并添加了若干方法后,可以使用 . 调用对象中函数,我称之为方法调用。
-
- 对象方法可以传递参数
操作对象
增加属性
新增对象中的方法
删除对象中的属性
- delete 对象.属性
遍历对象
动态渲染列表
定义一个存储了若干学生信息的数组
// let students = [{ name: '李狗蛋', age: 18, gender: '男', hometown: '广东省' }, { name: '张翠花', age: 19, gender: '女', hometown: '四川省' }, { name: '赵铁柱', age: 17, gender: '男', hometown: '广西省' }, { name: '钱多多', age: 18, gender: '女', hometown: '湖南省' }, { name: '松松老师', age: 18, gender: '女', hometown: '黑龙江省' }, { name: '班班', age: 18, gender: '女', hometown: '广东省' } ]
document.write(`<table><caption>
<h3>学生列表</h3>
</caption>
<tr>
<th>序号</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>家乡</th>
</tr>`)
for (let i = 0; i < students.length; i++) {
document.write(`
<tr>
<th>${i+1}</th>
<th>${students[i].name}</th>
<th>${students[i].age}</th>
<th>${students[i].gender}</th>
<th>${students[i].hometown}</th>
</tr>`)
}
document.write(`</table>`)
内置对象
随机数案例
- // 0 - 10的随机数 // Math.random() -> [0, 1) // Math.random()10 -> [0,10) // Math.random() (10 + 1) -> [0,11) // Math.floor( Math.random()* (10 + 1)) -> 从0 - 10的整数 console.log(Math.floor(Math.random() * 11));
// 5 - 10的随机数
// 0 - 5的随机数 + 5
console.log(Math.floor(Math.random() * (5 + 1)) + 5);
// n - m的随机数
// Math.floor(Math.random() * (m - n + 1)) + n
function getRANDOM() {
return Math.floor(Math.random() * (max - min + 1)) + min
}
console.log();
\