对象的基本使用
属性 和 方法
<script>
let obj = {
uname : 'ikun',
uage : 18 ,
'other-name' : '只因',
sing : function (){
document.write('唱')
},
jump : function (){
document.write('跳')
},
rap : function (){
document.write('rap')
},
hobby : function (x , y){
x = x || 0
y = y || 0
document.write(x + y)
}
}
document.write(obj.uname) //查
obj.gender = 'man' //增
document.write(obj.gender)
document.write(obj['gender'])
delete obj.gender //删
//属性中带有字符串的
document.write(obj['other-name'])
obj.sing()
obj.jump()
obj.rap()
obj.hobby()
obj.hobby(1,2)
</script>
对象(无序的)的属性遍历 for in
<script>
for (let k in obj) {
document.write(k) // 'uname' 'uage'
document.write(obj[k]) //属性值
}
</script>
对象数组
<script>
let students = [{uname : '蔡徐坤', age : 18 , hobby : '唱跳RAP篮球'},{uname : '只因', age : 3 ,hobby: '清蒸红烧白切'}]
for (let i = 0; i < students.length; i++) {
console.log(students[i])
console.log(students[i].uname)
console.log(students[i]['hobby'])
}
</script>
Math 内置函数
<script>
// Math.ceil() 向上取整
// Math.floor() 向下取整
// Math.round() 四舍五入
// Math.max()
// Math.min()
//Math.random [0,1)
//随机生成0-10
console.log(Math.floor(Math.random()*(10+1)))
console.log(Math.round(Math.random()*10))
//随机生成5-10
console.log(5+Math.floor(Math.random()*6))
//随机生成N-M
function getRandom(N,M){
N = N || 0
M = M || 0
return N+Math.floor(Math.random()*(M-N+1))
}
</script>