• 关于JS中的call()方法和apply() 暂时只接触到call() 等接触到apply()再回头来看


    1. 每个函数都包含两个非继承而来的方法:call()方法和apply()方法。

    2. 相同点:这两个方法的作用是一样的。

    都是在特定的作用域中调用函数,等于设置函数体内this对象的值,以扩充函数赖以运行的作用域。

    一般来说,this总是指向调用某个方法的对象,但是使用call()和apply()方法时,就会改变this的指向。

    call()方法使用示例:

     1     //例1
     2     <script>
     3         window.color = 'red';
     4         document.color = 'yellow';
     5 
     6         var s1 = {color: 'blue' };
     7         function changeColor(){
     8             console.log(this.color);
     9         }
    10 
    11         changeColor.call();         //red (默认传递参数)
    12         changeColor.call(window);   //red
    13         changeColor.call(document); //yellow
    14         changeColor.call(this);     //red
    15         changeColor.call(s1);       //blue
    16 
    17     </script>
    18 
    19     //例2
    20     var Pet = {
    21         words : '...',
    22         speak : function (say) {
    23             console.log(say + ''+ this.words)
    24         }
    25     }
    26     Pet.speak('Speak'); // 结果:Speak...
    27 
    28     var Dog = {
    29         words:'Wang'
    30     }
    31 
    32     //将this的指向改变成了Dog
    33     Pet.speak.call(Dog, 'Speak'); //结果: SpeakWang

    apply()方法使用示例:

     1  //例1
     2     <script>
     3         window.number = 'one';
     4         document.number = 'two';
     5 
     6         var s1 = {number: 'three' };
     7         function changeColor(){
     8             console.log(this.number);
     9         }
    10 
    11         changeColor.apply();         //one (默认传参)
    12         changeColor.apply(window);   //one
    13         changeColor.apply(document); //two
    14         changeColor.apply(this);     //one
    15         changeColor.apply(s1);       //three
    16 
    17     </script>
    18 
    19     //例2
    20     function Pet(words){
    21         this.words = words;
    22         this.speak = function () {
    23             console.log( this.words)
    24         }
    25     }
    26     function Dog(words){
    27         //Pet.call(this, words); //结果: Wang
    28        Pet.apply(this, arguments); //结果: Wang
    29     }
    30     var dog = new Dog('Wang');
    31     dog.speak();

    3. 不同点:接收参数的方式不同。

    • apply()方法 接收两个参数,一个是函数运行的作用域(this),另一个是参数数组。

    语法:apply([thisObj [,argArray] ]);,调用一个对象的一个方法,2另一个对象替换当前对象。

    说明:如果argArray不是一个有效数组或不是arguments对象,那么将导致一个 
    TypeError,如果没有提供argArray和thisObj任何一个参数,那么Global对象将用作thisObj。

    • call()方法 第一个参数和apply()方法的一样,但是传递给函数的参数必须列举出来。

    语法:call([thisObject[,arg1 [,arg2 [,...,argn]]]]);,应用某一对象的一个方法,用另一个对象替换当前对象。

    说明: call方法可以用来代替另一个对象调用一个方法,call方法可以将一个函数的对象上下文从初始的上下文改变为thisObj指定的新对象,如果没有提供thisObj参数,那么Global对象被用于thisObj。

    使用示例1:

    1   function add(c,d){
    2         return this.a + this.b + c + d;
    3     }
    4 
    5     var s = {a:1, b:2};
    6     console.log(add.call(s,3,4)); // 1+2+3+4 = 10
    7     console.log(add.apply(s,[5,6])); // 1+2+5+6 = 14 

    使用示例2:

     1 <script>
     2         window.firstName = "Cynthia"; 
     3         window.lastName = "_xie";
     4 
     5         var myObject = {firstName:'my', lastName:'Object'};
     6 
     7         function getName(){
     8             console.log(this.firstName + this.lastName);
     9         }
    10 
    11         function getMessage(sex,age){
    12             console.log(this.firstName + this.lastName + " 性别: " + sex + " age: " + age );
    13         }
    14 
    15         getName.call(window); // Cynthia_xie
    16         getName.call(myObject); // myObject
    17 
    18         getName.apply(window); // Cynthia_xie
    19         getName.apply(myObject);// myObject
    20 
    21         getMessage.call(window,"女",21); //Cynthia_xie 性别: 女 age: 21
    22         getMessage.apply(window,["女",21]); // Cynthia_xie 性别: 女 age: 21
    23 
    24         getMessage.call(myObject,"未知",22); //myObject 性别: 未知 age: 22
    25         getMessage.apply(myObject,["未知",22]); // myObject 性别: 未知 age: 22
    26 
    27     </script>
  • 相关阅读:
    [AT2064] [agc005_f] Many Easy Problems
    [AT2304] [agc010_c] Cleaning
    [AT2172] [agc007_e] Shik and Travel
    [AT2148] [arc063_c] Integers on a Tree
    [AT2363] [agc012_c] Tautonym Puzzle
    未能从程序集“netstandard, Version=2.0.0.0......”中加载类型“...”。
    Android Studio设置国内镜像代理
    新建一个浏览器APP
    Android Studio在Make Project时下载Grandle特别慢
    用JS添加和删除class类名
  • 原文地址:https://www.cnblogs.com/likewpp/p/9599515.html
Copyright © 2020-2023  润新知