JS高级02

本文介绍了JavaScript中构造函数的求和方法,原型对象的继承机制,call方法的使用,以及如何通过call实现继承。此外,还详细讲解了forEach方法用于遍历数组并进行操作,如求和与页面渲染,以及filter方法在过滤数组中的应用。最后讨论了字符串处理,如去除空格,以及map方法用于批量修改数组元素内容。
摘要由CSDN通过智能技术生成

一、构造函数求和

<body>
    
    <script>
        Array.prototype.sum=function(){
            // this指向创建的对象
            // 初始化一个变量
            var sum=0
            for(var i=0;i<this.length;i++){
                sum+=this[i]
            }
            console.log(this);//[1, 2, 3]
            return sum
        }
        var arr = new Array(1,2,3)
        console.log(arr);
        console.log(arr.sum());
    </script>
    <script>
        Object.prototype.sum=function(){
            // this指向创建的对象
            // 初始化一个变量
            var sum=0
            for(var i=0;i<this.length;i++){
                sum+=this[i]
            }
            console.log(this);//[1, 2, 3]
            return sum
        }
        var arr = new Array(1,2,3)
        console.log(arr);
        console.log(arr.sum());
    </script>
</body>

二、原型对象继承

<body>

    <script>
        function Father(uname,age){
            this.uname=uname
            this.age=age
        }
        Father.prototype.money = function(){
            alert('1000万')
        }

        function Son(uname,age){
            this.uname=uname
            this.age=age
        }  
        // 儿子想继承爸爸的1000万
        // Son.prototype.money=function(){
        //     alert('1000万')
        // }
        // 赋值给儿子的原型对象
        // Son.prototype=Father.prototype //Father.prototype
        Son.prototype=new Father('董事长',50) //Father.prototype
        // 手动指回son构造函数
        Son.prototype.constructor=Son
        var father = new Father('董事长',50)     
        var son = new Son('富二代',20)  
        console.log(father);   
        console.log(son);
        son.money()   
    </script>
</body>

三、call方法

<body>
    
    <script>
        /* 
        call()
        call可以调用函数(立即调用函数),改变函数的this指向,call(函数需要指向谁就写谁,参数1,参数2...)
        */
    //    普通函数this指向window
       function fn(x,y){
        console.log(x+y)
        console.log(this)
       }
       var obj = {
        name:'王多磊'
       }
    //    fn(1,2)
    console.log(obj)//this指向window
    fn.call(obj,1,2)//this指向obj
    </script>
</body>

四、使用call继承

<body>

    <script>
        // 父构造函数
        function Father(uname,age){
            this.uname=uname
            this.age=age
        }
        Father.prototype.money = function(){
            alert('1000万')
        }
        // 子构造函数
        function Son(uname,age,sex){
            Father.call(this,uname,age)
            // this.uname=uname
            // this.age=age
            this.sex=sex
        }  

        // 赋值给儿子的原型对象
        Son.prototype=Father.prototype //Father.prototype
        // 手动指回son构造函数
        Son.prototype.constructor=Son
        var father = new Father('董事长',50)     
        var son = new Son('富二代',20,'女')  
        console.log(father);   
        console.log(son);
        // son.money()   
    </script>
</body>

五、forEach遍历数组

<body>
    
    <script>
        // 数组里面可以写任意类型数据
        // 实际开发中数组基本上存储的都是对象形式的数据
        // 数组中的对象属性,如果想要拿到对象属性的话可以通过arr[1].name拿到

        // var arr = ['1',1,true,{}]
        var arr = [
                {name:'li'},
                {name:'wang'},
                {name:'zhang'}
        ]
        console.log(arr.length);//3
        console.log(arr[1].name);//wang

        // forEach遍历数组,array代表需要遍历的数组 item:数组中的元素(必须项) index:数组元素的索引(可选项)
        // 语法:array.forEach(function(item,index){})
        // forEach就是单纯的遍历数组,没有返回值

        var arr = [1,2,3,4]
        var sum = 0
        arr.forEach(function(item,index){
            sum+=item
        })
        console.log(sum);//10
    </script>
</body>

六、使用forEach渲染页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        li {
            width: 200px;
            list-style: none;
            float: left;
        }
        *{
            padding: 0;
            margin: 0;
        }
    </style>
</head>
<body>
    <ul>

    </ul>
    
    <script>
        var arr = [
                {img:'1.webp',name:'li',price:'¥1999'},
                {name:'zhou',price:'¥788'},
                {name:'wang',price:'¥599'},
                {name:'zhao',price:'¥109'},
        ]

        var ul = document.querySelector('ul')
        arr.forEach(function(item,index){
            var li = document.createElement('li')
            ul.appendChild(li)
            li.innerHTML='<p>'+item.name+'</p>'+"<img src="+item.img+">"+'<p>'+item.price+'</p>'
        })
    </script>
</body>
</html>

七、filter过滤数组

<body>
    
    <script>
        var arr = [12,53,5435,345,523]
        /* 
        filter 过滤器 返回符合条件的数据组成一个新数组(返回一个新数组)
        item 代表数组中的元素 index代表数组索引
        Array.filter(function(item,index){})
        */
       var arr1=arr.filter(function(item,index){
        console.log(item,index)
        return item>50
       })
       console.log(arr1);
       console.log(arr);
    </script>
</body>
</html>

八、filter案例

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .main {
            width: 500px;
            height: 100px;
            margin: 0 auto;
        }

        .box {
            width: 200px;
            height: 500px;
            margin: 0 auto;
            overflow-y: auto;
        }
    </style>
</head>

<body>
    <div class="main">
        请输入手机价格(输入能接受的最高价格): <input type="text">
    </div>
    <div class="box">

    </div>

    <script>
        var arr = [
            {
                name: '小米',
                price: 2999,
            },
            {
                name: '红米',
                price: 2000,
            },
            {
                name: '魅族',
                price: 2599,
            },
            {
                name: '华为',
                price: 7999,
            },
            {
                name: '荣耀',
                price: 2899,
            },
            {
                name: '联想',
                price: 999,
            },
            {
                name: '三星',
                price: 9999,
            },
            {
                name: '苹果',
                price: 7999,
            },
            {
                name: '诺基亚',
                price: 299,
            },
            {
                name: 'vivo',
                price: 2399,
            },
            {
                name: 'oppo',
                price: 2499,
            },
        ]

        var box = document.querySelector('.box')
        var input = document.querySelector('input')
        input.onblur = function () {
            // box.innerHTML=''
            var newArr=[]
            newArr = arr.filter(function (item) {
                return item.price < input.value
            })
            console.log(newArr)
            if (newArr.length < 1) return alert('没有符合的商品')
            box.innerHTML=''
            newArr.forEach(function (item) {
                
                var div = document.createElement('div')
                box.appendChild(div)
                div.innerHTML = '<p>' + item.name + '</p><p>' + item.price + '</p>' + '<br/>'
            })
        }
    </script>
</body>

</html>
    <script>
        // 民政局决定分配对象,凡是年龄大于20岁都可以分配一个白富美

        //  filter筛选出来的数据是数组中每条数据内所有的内容,不是单一返回筛选的某个条件
        var arr = [
            {
                id: 1,
                name: '张三',
                age: 20
            },
            {
                id: 2,
                name: '李四',
                age: 18
            },
            {
                id: 3,
                name: '王五',
                age: 22
            },
            {
                id: 4,
                name: '麻子',
                age: 21
            },
        ]
        var arr1 = arr.filter(function (item) {
            return item.age > 19
        })
        console.log(arr1)

        //    找到张三和李四的数据,把他们的id转化为'1,3'字符串
        var arr2 = arr.filter(function (item) {
            return item.name == '张三' || item.name == '王五'
        })
        console.log(arr2)
        var arr3 = []
        arr2.forEach(function (item) {
            arr3.push(item.id)
        })
        console.log(arr3)
        console.log(arr3.join(','))
    </script>

九、every遍历数组

    <script>
        // every 检查数组中是否有满足条件的元素,如果有不符合条件的会返回false,只有全部符合条件才会返回true
        // 特点:只要找到不符合条件的数据就会终止查找(遍历打印的时候包括不符合条件的数据),直接返回结果
        var arr = [37,66,35,63,88]
        var flag = arr.every(function(item,index){
            console.log(item,index);
            return item>35
        })
        console.log(flag);//false
    </script>

十、some遍历数组

    <script>
        // some 检查数组中是否有满足条件的元素,如果有一个或以上就会返回true,否则返回false
        // 特点:只要找到符合条件的数据就会终止查找(遍历打印的时候包括符合条件的第一个数据),直接返回结果
        var arr = [37,35,66,63,88]
        var flag = arr.some(function(item,index){
            console.log(item,index);
            return item>50
        })
        console.log(flag);//true
    </script>

十一、reduce累加器

    <script>
        // reduce 累加器
        // Array.reduce(function(上一次的值,当前的值){},起始值)没有起始值默认为0
        var arr = [1,2,3,4,5]
        var sum = arr.reduce(function(old,now){
            return old+=now
        },10)
        console.log(sum);//25
    </script>

十二、find遍历数组

    <script>
        // find 返回满足条件的第一个元素,如果查不到符合条件的元素就会返回undefined
        // 只要查到有满足条件的元素就会终止查询(遍历打印的时候一直到(包括)符合条件的数据),直接返回符合条件的元素

        var arr = [1,2,3,4,5,6,7,8]
        var a = arr.find(function(item,index){
            console.log(item,index);
            return item>5
            // return item>10//undefined
        })
        console.log(a);//6
    </script>

十三、findIndex遍历数组

    <script>
        // findIndex 查找符合条件元素的下标,只返回第一个符合条件元素的下标,只要找到符合条件的元素就会终止查询
        // (遍历打印的时候一直到(包括)符合条件的数据),如果找不到符合条件的就会返回-1.和indexOf类似
        var arr = [1,2,3,4]
        var a = arr.findIndex(function(item,index){
            console.log(item,index);
            // return item>2//2
            return item>3//3
            // return item>6//-1
        })
        console.log(a);
    </script>

十四、去除字符串空格

<body>
    <input type="text">

    <script>
        // trim() 去除字符串前后的空格,中间的空格会默认为成字符,不去除
       var str=' he llo '
       var str1='hello'
       console.log(str.trim())
       console.log(str1)
    var input=document.querySelector('input')
    input.onblur=function(){
        console.log(input.value.trim())
        if(input.value.trim()){
            alert('我有值')
        }else{
            alert('请输入内容')
        }
    }
    </script>
</body>

十五、map批量修改数组元素内容

    <script>
        // map 对原始数组的一一映射,批量修改数组中的元素就会用到map,会返回一个新数组(不会改变原来的数组)
        var arr = [2,3,4,5]
        var newArr=arr.map(function(item,index){
            // console.log(item,index);
            return item*2
        })
        console.log(newArr.join());
    </script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值