六 JavaScript基础之ECMAScript 对象

62 阅读1分钟

对象

js中数据类型,无序的数据集合,详细描述某事物信息

声明对象

   let obj = {}
   //第二种方式
   let obj = new Object()

属性(名词)

key:'value' 属性名:‘属性值’,

   let obj = {
       uname:'靓仔',
       age:'18',
       gender:'male'
    }

方法(动词)

本质是函数 可添加形参与实参 方法名:function(){ }

调用 对象名.方法名()

   let obj = {
       uname:'liangzai',
       song:function(){
          console.log('执行方法')
       }
   }
   obj.song()

数据操作

增删改查

对象名.属性名

对象名['属性名']

对象名.属性名 = 新值

对象名.新属性名 = 新值

delete 对象名.属性名

遍历对象 for in

for(let k in 对象名){console.log(对象名[k]) }

数组对象

let obj = [{},{},{}]

遍历数组对象

for(let i=0;i<arr.length;i++){arr[i].属性名}

数组对象案例--循环渲染页面学生信息表格

截屏2023-04-24 16.44.17.png

//表格样式
 <style>
        table{
            margin: 50px auto;
            border-collapse: collapse;
        }
        th{
            background-color: darkgrey;
        }
        th,td{
             padding: 10px 20px;
             border: 1px solid #ccc;
             text-align: center;
        }
  </style>
  
<table>
            <tr>
                <th>序号</th>
                <th>姓名</th>
                <th>年龄</th>
                <th>性别</th>
                <th>家乡</th>
            </tr>            
            <script>
                let students = [
                    {name:'小明',age:18,gender:'男',hometown:'河北省'},
                    {name:'小红',age:19,gender:'女',hometown:'河南省'},
                    {name:'小刚',age:17,gender:'男',hometown:'山西省'},
                    {name:'小丽',age:18,gender:'女',hometown:'山东省'},
                ]
            for(let i=0;i<students.length;i++){
            document.write(`
            <tr>
                <td>${i+1}</td>
                <td>${students[i].name}</td>
                <td>${students[i].age}</td>
                <td>${students[i].gender}</td>
                <td>${students[i].hometown}</td>
            </tr>
            `)}
        </script>
</table>

内置对象 Math对象

属性 Math.PI

方法 Math.ceil() Math.floor() Math.round() Math.max() Math.min() Math.random()

null

null=== let obj ={}
null是空对象

            0-10的随机数 Math.floor(Math.random()*(10+1))
            5-10的随机数 Math.floor(Math.random()*(5+1))+5
            N-M的随机数  Math.floor(Math.random()*(M-N+1))+N
            数组下标随机数 Math.floor(Math.random()*arr.length)

数组案例--随机抽取元素且不重复该元素

            let arr = ['黄忠','关羽','赵云','张飞','马超','曹超']
            function getRandom(){
                return  Math.floor(Math.random()*arr.length)
            }
            let random = getRandom()
            document.write(arr[random])
            //数组中删除选中元素不重复
            arr.splice(random,1)
        

案例--猜数字游戏 随机生成1-10数字猜

            function getRandom(N,M){
                return  Math.floor(Math.random()*(M-N+1))+N
            }
            let random = getRandom(1,10)
// while循环和for循环的区别: for循环次数是确定的,while循环次数不确定,无限循环满足条件退出
            
            //不限定次数猜数字,直到猜对了退出
            while(true){
            let a = +prompt('请输入您猜的数字')
            if(a>random){
                alert('数字猜大了,继续猜')
            }else if(a < random){
                alert('数字猜小了,继续猜')
            }else{
                alert('恭喜您猜对了')
                break
            }} 
            //限定猜次数3次 没猜对直接退出或猜对退出
            let flag = true //开关变量
            for(let i=1;i<=3;i++){
                let a = +prompt('请输入您猜的数字')
            if(a>random){
                alert('数字猜大了,继续猜')
            }else if(a < random){
                alert('数字猜小了,继续猜')
            }else{
                flag = false
                alert('恭喜您猜对了')
                break
            }
           } 
             if(flag){
                alert('次数已经用完了')
             } 

案例--随机生成颜色

           function getRandomColor(flag=true){
                 if(flag){
               // 随机生成0-f之间的#ffffff 16进制颜色格式
                    let str = '#'
 let arr =['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f']
                    for(let i=0;i<6;i++){
                        let random = Math.floor(Math.random()*arr.length)
                        //累加6位元素
                        str += arr[random]
                    }
                    return str
                }else{  
                    // 随机生成rgb颜色
                    let r = Math.floor(Math.random()*(255+1))
                    let g = Math.floor(Math.random()*(255+1))
                    let b = Math.floor(Math.random()*(255+1))
                    return `rgb(${r},${g},${b})`
                }
             }
             console.log(getRandomColor())

综合案例

数组对象渲染网站首页

截屏2023-04-24 16.55.56.png

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
       *{
        margin: 0;
        padding: 0;
       }
       .w{
        width: 1200px;
        margin: auto;
       }
       body{
        background-color: #f3f5f7;
       }
       li{
        list-style: none;
       }
       a{
        text-decoration: none;
       }
       .clearfix:before,.clearfix:after{
        content: "";
        display: table;
       }
       .clearfix:after{
        clear: both;
       }
       .clearfix{
        *zoom:1;
       }
       .box{
        margin-top: 30px;
       }
       .box-hd{
        height: 45px;
       }
       .box-hd h3{
        float: left;
        font-size: 20px;
        color: #494949;
       }
       .box-hd a{
        float: right;
        font-size: 12px;
        color: #a5a5a5;
        margin-top: 10px;
        margin-right: 30px;
       }

       .box-bd ul{
        width: 1225px;
       }
       .box-bd ul li{
        position: relative;
        top: 0;
        float: left;
        width: 228px;
        height: 270px;
        background-color: #fff;
        margin-right: 15px;
        margin-bottom: 15px;
        transition: all .3s;
       }
       .box-bd ul li a{
        display: block;
       }
       .box-bd ul li:hover{
        top: -8px;
        box-shadow: 0 15px 30px rbg(000/10%);
       }
       .box-bd ul li img{
        width: 100%;
       }
       .box-bd ul li h4{
        margin: 20px 20px 20px 25px;
        font-size: 14px;
        color: #050505;
        font-weight: 400;
       }
       .box-bd .info{
        margin: 0 20px 0 25px;
        font-size: 12px;
        color: #999;
       }
       .box-bd .info span{
        color: #ff7c2d;
       }
    </style>
</head>
<body>
    <div class="box w">
        <div class="box-hd">
        <h3>精品推荐</h3>
        <a href="#javascript;">查看全部</a>
        </div>
        <div class="box-bd">
           <ul class="clearfix">
            <script>
                let data = [
                        {
                            src: 'images/course01.png',
                            title: 'Think PHP 5.0 博客系统实战项目演练',
                            num: 1125
                        },
                        {
                            src: 'images/course02.png',
                            title: 'Android 网络动态图片加载实战',
                            num: 357
                        },
                        {
                            src: 'images/course03.png',
                            title: 'Angular2 大前端商城实战项目演练',
                            num: 22250
                        },
                        {
                            src: 'images/course04.png',
                            title: 'Android APP 实战项目演练',
                            num: 389
                        },
                        {
                            src: 'images/course05.png',
                            title: 'UGUI 源码深度分析案例',
                            num: 124
                        },
                        {
                            src: 'images/course06.png',
                            title: 'Kami2首页界面切换效果实战演练',
                            num: 432
                        },
                        {
                            src: 'images/course07.png',
                            title: 'UNITY 从入门到精通实战案例',
                            num: 888
                        },
                        {
                            src: 'images/course08.png',
                            title: Cocos 深度学习你不会错过的实战',
                            num: 590
                        },
                    ]
                for(let i=1;i<=data.length;i++){  
                    document.write(`
                    <li>
                        <a href="#">
                            <img src="${data[i].src}" alt="${data[i].title}">
                            <h4>${data[i].title}</h4>
                            <div class="info">
                                <span>高级</span> . <span>${data[i].num}</span>人在学习
                            </div>
                        </a>
                    </li>
                `)}
            </script>
           </ul>
        </div>
    </div>
</body>
</html>

案例 -- 上面重构版

  <script>
        // 1. 重构  
        let data = [
            {
                src: 'images/course01.png',
                title: 'Think PHP 5.0 博客系统实战项目演练',
                num: 1125
            },
            {
                src: 'images/course02.png',
                title: 'Android 网络动态图片加载实战',
                num: 357
            },
            {
                src: 'images/course03.png',
                title: 'Angular2 大前端商城实战项目演练',
                num: 22250
            },
            {
                src: 'images/course04.png',
                title: 'Android APP 实战项目演练',
                num: 389
            },
            {
                src: 'images/course05.png',
                title: 'UGUI 源码深度分析案例',
                num: 124
            },
            {
                src: 'images/course06.png',
                title: 'Kami2首页界面切换效果实战演练',
                num: 432
            },
            {
                src: 'images/course07.png',
                title: 'UNITY 从入门到精通实战案例',
                num: 888
            },
            {
                src: 'images/course08.png',
                title: 'Cocos 深度学习你不会错过的实战',
                num: 590
            },
        ]
        
    for(let i = 0;i<data.length;i++){
       const ul = document.querySelector('.clearfix')
       const li = document.createElement('li')
      
        li.innerHTML = `
              <a href="#">
                    <img src=${data[i].src} alt="">
                    <h4>
                        ${data[i].title}
                    </h4>
                    <div class="info">
                        <span>高级</span> • <span>${data[i].num}</span>人在学习
                    </div>
                </a>
        `
        ul.appendChild(li)
    }
    </script>