JS基础--对象

对象的基本使用

属性 和 方法

<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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值