爱编程的设计师 2022-06-05 21:54 采纳率: 94.4%
浏览 31
已结题

str调用的foreach方法里面的this为什么不是指向str,不是谁调用this指向谁吗

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
<!DOCTYPE html>
<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>
        .main {
            margin: 100px auto;
            width: 500px;
            height: 500px;
            background-color: rgb(158, 164, 162);
            overflow: hidden;
        }

        .father {
            margin: 30px auto;
            width: 200px;
            height: 200px;
            margin-bottom: 20px;
            background-color: blueviolet;
        }

        .father div {
            width: 30px;
            height: 30px;
            margin-bottom: 5px;
            background-color: yellow;
        }

        .mother {
            margin: auto;
            width: 200px;
            height: 200px;
            background-color: blue;

        }

        .mother div {
            width: 30px;
            height: 30px;
            margin-bottom: 5px;
            background-color: yellowgreen;
        }
    </style>
</head>

<body>
    <div class="home">
        <div class="main">
           
            <div class="father" id="worker">
                <div></div>
                <div class="top"></div>
                <div class="buttom"></div>
                <span>one</span>
                <span>two</span>
                <span>three</span>
            </div>
            
            <div class="mother">
                <div></div>
                <div></div>
                <div></div>
                <span>four</span>
                <span>five</span>
                <span>six</span>
            </div>
        </div>
        <div class="main"></div>
        <div class="main">three</div>
        <div class="main"></div>
        <div class="main"></div>
        <div class="main"></div>
        <div class="main"></div>
    </div>

    <script>
        let father = document.querySelector('.father');
        let arr = father.querySelectorAll('div');
        var one = arr[0];
        var col = window.getComputedStyle(one).backgroundColor;
        var str = col.split(',');
        function extract(str){
            var num_arr=[];
            var ii = 0;
            var stri = '';
            for(var i = 0;i<str.length;i++){
                var cont = str.charAt(i);
                if(!isNaN(cont)){num_arr[ii]=cont;ii++}
            }
            num_arr.forEach((item)=>{
                stri = stri+ item;
            })
            return stri;
        }
        str.forEach((item,index)=>{
            str[index]=extract(item);
            // this[index]=extract(item);
            console.log(str[index],this[index]);
        })
        console.log(str);
    </script>
</body>

</html>
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/686781734456158.png "#left")


img

  • 写回答

3条回答 默认 最新

  • Heerey525 前端领域新星创作者 2022-06-05 22:12
    关注

    str是数组,普通函数确实可以说是谁调用this就指向谁
    在这里forEach中箭头函数没有没有自己的this,向上一级作用域找,所以是全局的window对象

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 6月13日
  • 已采纳回答 6月5日
  • 修改了问题 6月5日
  • 创建了问题 6月5日

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。