js中apply和call方法

來源:互聯網
上載者:User

標籤:htm   prototype   null   window   html   var   class   person   使用   

1.apply的使用,call的使用改變this對象
//apply和call使用function Person(age,sex){this.age=age;this.sex=sex;}//通過原型添加方法Person.prototype.sayHi=function(x,y){console.log("你好啊"+this.sex);return 1000;}var person=new Person(10,"男");person.sayHi();//function Student(name,sex){this.name=name;this.sex=sex;}//此時改變了原型對象的問題,var stu=new Student("小明","人妖");var r1=person.sayHi.apply(stu,[10,20]);//改變了this對象給stu添加了sayHi對象var r2=person.sayHi.call(stu,10,20);

  

2.apply和call使用總結
 //apply和call的使用方法    /*    * apply的使用文法    * 函數名字.apply(對象,[參數1,參數2,...]);    * 方法名字.apply(對象,[參數1,參數2,...]);    * call的使用文法    * 函數名字.call(對象,參數1,參數2,...);    * 方法名字.call(對象,參數1,參數2,...);    *    * 作用:改變this的指向    * 不同的地方:參數傳遞的方式是不一樣的    *    * 只要是想使用別的對象的方法,並且希望這個方法是當前對象的,那麼就可以使用apply或者是call的方法改變this的指向    *    * *///apply和call都可以改變this的指向                                                            //函數的調用,改變this的指向                                                                   function f1(x,y) {                                                                console.log((x+y)+":===>"+this);                                                return "這是函數的傳回值";                                                            }                                                                               //apply和call調用                                                                  var r1=f1.apply(null,[1,2]);//此時f1中的this是window                                 console.log(r1);                                                                var r2=f1.call(null,1,2);//此時f1中的this是window                                    console.log(r2);                                                                console.log("=============>");                                                 // 改變this的指向                                                                     var obj={                                                                         sex:"男"                                                                       };                                                                            //  本來f1函數是window對象的,但是傳入obj之後,f1函數此時就是obj對象的                                     var r3=f1.apply(obj,[1,2]);//此時f1中的this是obj                                     console.log(r3);                                                                var r4=f1.call(obj,1,2);//此時f1中的this是obj                                        console.log(r4);                                                                                                                                      

 

js中apply和call方法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.