Js中this的指向问题(重点)

在js中在不同的函数环境执行,this的指向的对象是不同的,这一点我们要清楚的了解,只有清楚this的指向问题,我们写的代码才能更精简实用,减少定义不必要的变量,少开辟内存空间。
函数的调用方式大概分为6种,不同的函数,里面的this指向是不同的,接下来我为大家一一做实例解释。
在这里插入图片描述

代码实例演示:

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <button>点击</button>
    <script>
        // 函数的不同调用方式决定了this 的指向不同
        // 1. 普通函数 this 指向window
        function fn() {
    
    
            console.log('普通函数的this' + this);
        }
        window.fn();
        // 2. 对象的方法 this指向的是对象 oobj
        var obj = {
    
    
            sayHi: function() {
    
    
                console.log('对象方法的this:' + this);
            }
        }
        obj.sayHi();
        // 3. 构造函数 this 指向 ldh 这个实例对象 原型对象里面的this 指向的也是 ldh这个实例对象
        function Star() {
    
    };
        Star.prototype.sing = function() {
    
    
           console.log('构造函数方法的this:' +this)
        }
        var user= new Star();
        // 4. 绑定事件函数 this 指向的是函数的调用者 btn这个按钮对象
        var btn = document.querySelector('button');
        btn.onclick = function() {
    
    
            console.log('绑定时间函数的this:' + this);
        };
        // 5. 定时器函数 this 指向的也是window
        window.setTimeout(function() {
    
    
            console.log('定时器的this:' + this);

        }, 1000);
        // 6. 立即执行函数 this还是指向window
        (function() {
    
    
            console.log('立即执行函数的this' + this);
        })();
    </script>
</body>

</html>

当然我们也可以改变函数的this指向,改变函数this指向的方法有3个,分别是call,apply,bind,不同的方法作用不同,具体要以哪一个,要看实际情况。

第一个:call方法

主要用于继承父类

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        // 改变函数内this指向  js提供了三种方法  call()  apply()  bind()

        // 1. call()
        var o = {
    
    
            name: 'andy'
        }

        function fn(a, b) {
    
    
            console.log(this);
            console.log(a + b);

        };
        fn.call(o, 1, 2);
        // call 第一个可以调用函数 第二个可以改变函数内的this 指向
        // call 的主要作用可以实现继承
        function Father(uname, age, sex) {
    
    
            this.uname = uname;
            this.age = age;
            this.sex = sex;
        }

        function Son(uname, age, sex) {
    
    
            Father.call(this, uname, age, sex);
        }
        var son = new Son('小明', 18, '男');
        console.log(son);
    </script>
</body>

</html>

第二个:apply方法

主要应用于数组(伪数组也行),因为这个方法的参数就是一个数组。
实例演示:

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        // 改变函数内this指向  js提供了三种方法  call()  apply()  bind()

        // 2. apply()  应用 运用的意思
        var o = {
    
    
            name: 'andy'
        };

        function fn(arr) {
    
    
            console.log(this);
            console.log(arr); // 'pink'

        };
        fn.apply(o, ['pink']);
        // 1. 也是调用函数 第二个可以改变函数内部的this指向
        // 2. 但是他的参数必须是数组(伪数组)
        // 3. apply 的主要应用 比如说我们可以利用 apply 借助于数学内置对象求数组最大值 
        // Math.max();
        var arr = [1, 66, 3, 99, 4];
        var arr1 = ['red', 'pink'];
        // var max = Math.max.apply(null, arr);
        var max = Math.max.apply(Math, arr);
        var min = Math.min.apply(Math, arr);
        console.log(max, min);
    </script>
</body>

</html>

第三个:bind方法

主要用于,我们不想调用原函数,只想改变原函数的this指向,因为前2个方法都会去调用函数的,而这个方法不会。如果有的函数我们不需要立即调用,但是又想改变这个函数内部的this指向此时用bind方法。
实例:

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <button>点击</button>
    <button>点击</button>
    <button>点击</button>
    <script>
        // 改变函数内this指向  js提供了三种方法  call()  apply()  bind()

        // 3. bind()  绑定 捆绑的意思
        var o = {
    
    
            name: 'andy'
        };

        function fn(a, b) {
    
    
            console.log(this);
            console.log(a + b);


        };
        var f = fn.bind(o, 1, 2);
        f();
        // 1. 不会调用原来的函数   可以改变原来函数内部的this 指向
        // 2. 返回的是原函数改变this之后产生的新函数
        // 3. 如果有的函数我们不需要立即调用,但是又想改变这个函数内部的this指向此时用bind
        // 4. 我们有一个按钮,当我们点击了之后,就禁用这个按钮,3秒钟之后开启这个按钮
        // var btn1 = document.querySelector('button');
        // btn1.onclick = function() {
    
    
        //     this.disabled = true; // 这个this 指向的是 btn 这个按钮
        //     // var that = this;
        //     setTimeout(function() {
    
    
        //         // that.disabled = false; // 定时器函数里面的this 指向的是window
        //         this.disabled = false; // 此时定时器函数里面的this 指向的是btn
        //     }.bind(this), 3000); // 这个this 指向的是btn 这个对象
        // }
        var btns = document.querySelectorAll('button');
        for (var i = 0; i < btns.length; i++) {
    
    
            btns[i].onclick = function() {
    
    
                this.disabled = true;
                setTimeout(function() {
    
    
                    this.disabled = false;
                }.bind(this), 2000);
            }
        }
    </script>
</body>

</html>

猜你喜欢

转载自blog.csdn.net/m0_46188681/article/details/107102436