美文网首页
JS 改变 this 指向对象

JS 改变 this 指向对象

作者: 夹板儿孩 | 来源:发表于2020-03-12 17:27 被阅读0次

今天在抽代码封装到原型对象中时发现了this指向不是我想要的结果,刚开始都不知道该怎么百度,在放弃时发现,原来this指向还能改!于是有了后面的例子。(代码中最后三行 JS)

<!doctype html>
<html lang="ZH">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        input {
            width: 100px;
            height: 100px;
        }
    </style>
</head>
<body>
//语法 fn.call(obj,n,n,...,n); <br>
//语法 fn.apply(obj,[n,n,...,n]);<br>
//语法 fn.bind(obj,n,n,...,n)();  最后的()不加,方法不会被调用 <br>
<br>
<input type="button" value="原来" onclick="input0('我是','input0')">
<input type="button" value="方式1" onclick="input1.call(this, '我是','input1')"/>       <!--第一个参数是需要将指针指向的位置,后面的参数是需要传到方法中的参数,逗号分隔追加-->
<input type="button" value="方式2" onclick="input2.apply(this, ['我是','input2'])"/>    <!--第一个参数是需要将指针指向的位置, 第二个参数是需要传到方法中的参数,所有参数都需要放到[]中,用逗号分隔-->
<input type="button" value="方式3" onclick="input3.bind(this, '我是','input2')()"/>     <!-- 最后一对()不加方法不会调用    第一个参数是需要将指针指向的位置,后面的参数是需要传到方法中的参数,逗号分隔追加-->
    <script>
        function input0(a, b){
            console.log(this);
            alert(a + ":" + b + "   value为:" + this.value);
        }
        function input1(a, b){
            console.log(this);
            alert(a + ":" + b + "   value为:" + this.value);
        }
        function input2(a, b){
            console.log(this);
            alert(a + ":" + b + "   value为:" + this.value);
        }
        function input3(a, b){
            console.log(this);
            alert(a + ":" + b + "   value为:" + this.value);
        }
        //========================其他例子分割线========================
        var a = "a";
        let s = "s";
        var data = {
            a: 1,
            b: 2,
            c: 'c'
        }
        //指向位置时第一个参数不一定非要用this,还可以指向别处
        // test1 中只示范 this 的指向,不解释原理。【【此方法并未更改 this 指向】】
        function test1() {
            let a = 0;
            console.log("a = " + a);            //打印 a = 0
            console.log("this.a = " + this.a);  //打印 this.a = a         |因为当前的this指向为 Window 对象
            console.log("s = " + s);            //打印 s = s
            console.log("this.s = " + this.s);  //打印 this.s = undefined |因为当前this指向为 Window 对象, let 全局变量不在 Window中,所以找不到 s 这个变量
                                                //参考 https://blog.csdn.net/qq_41257129/article/details/100072301
        }
        test1();
        console.log("================================================================")
        //调用此方法时更改this指向对象
        function test2(str){
            console.log("=================== " + str + " ===================");
            console.log("a = " + a);            //打印 a = 0         |没用 this 时,切函数内未定义 a 这个变量。则打印的是全局中的 a 变量值
            console.log("this.a = " + this.a);  //打印 this.a = 1    |我们在调用test2时将this指向更改到了 data 对象中,所以 a = 1
            console.log("this.b = " + this.b);  //打印 this.b = 2    |我们在调用test2时将this指向更改到了 data 对象中,所以 b = 2
            console.log("this.c = " + this.c);  //打印 this.c = c    |我们在调用test2时将this指向更改到了 data 对象中,所以 c = c
        }
        test2.call(data, "调用方式 test2.call(data,n,n,..,n)");         //语法 fn.call(obj,n,n,...,n);
        test2.apply(data,['调用方式 test2.apply(data,[n,n,..,n])']);    //语法 fn.apply(obj,[n,n,...,n]);
        test2.bind(data, "调用方式 test2.bind(data,n,n,..,n)()")();     //语法 fn.bind(obj,n,n,...,n)();  最后的()不加,方法不会被调用
    </script>
</body>
</html>

相关文章

  • JS 改变 this 指向对象

    今天在抽代码封装到原型对象中时发现了this指向不是我想要的结果,刚开始都不知道该怎么百度,在放弃时发现,原来th...

  • 面试题集

    简述JS中this的指向和如何改变它的指向 javascript中,this是动态绑定的,它可以是全局对象、当前对...

  • JS中的call()方法和apply()方法

    一、JS中this总是指向调用某个方法的对象,但是我们可以使用call()或apply()来改变this的指向 1...

  • 改变this指向之call()、apply()、bind()简单

    本节将对 js 内置对象 Function 中改变 this 指向的方法进行简单实现,在这之前先复习一下改变 ja...

  • 面试题的总结与解答

    1,js中this指向的理解 在js面向对象编程里我们避免不了使用this,所以理解this指向对于在面向对象编程...

  • JavaScript中apply、call和bind的区别

    js中改变this指向的方式主要有三种:apply、call和bind 相同点:都是用来改变函数的this对象的指...

  • java 有关this,apply使用

    js的call js的this指向改变 使用 java 的 apply

  • JS 对象指向相关

    0.引言:对象引用的原理: 首先,我们知道,创建对象有两种方法:1、字面量直接创建。 2、构造函数法创建对象。 其...

  • javascript this指向

    首页,js默认的对象是Window,调用一个函数是,如果不去改变它的调用对象的话,this基本上都会指向Windo...

  • 01基础

    immutable意义 它是提供了一种不可改变的数据结构 使用场景 js中对象都是参考类型,当a,b都指向某个对象...

网友评论

      本文标题:JS 改变 this 指向对象

      本文链接:https://www.haomeiwen.com/subject/jmgmjhtx.html