<轉> apply 和 call 的區別

來源:互聯網
上載者:User

標籤:屬性   數列   script   actions   使用   asc   bin   issues   highlight   

原味地址:https://github.com/lin-xin/blog/issues/7apply 和 call 的區別

ECMAScript 規範給所有函數都定義了 call 與 apply 兩個方法,它們的應用非常廣泛,它們的作用也是一模一樣,只是傳參的形式有區別而已。

apply( )

apply 方法傳入兩個參數:一個是作為函數內容相關的對象,另外一個是作為函數參數所組成的數組。

var obj = {    name : ‘linxin‘}function func(firstName, lastName){    console.log(firstName + ‘ ‘ + this.name + ‘ ‘ + lastName);}func.apply(obj, [‘A‘, ‘B‘]);    // A linxin B

 

可以看到,obj 是作為函數內容相關的對象,函數 func 中 this 指向了 obj 這個對象。參數 A 和 B 是放在數組中傳入 func 函數,分別對應 func 參數的列表元素。

call( )

call 方法第一個參數也是作為函數內容相關的對象,但是後面傳入的是一個參數列表,而不是單個數組。

var obj = {    name: ‘linxin‘}function func(firstName, lastName) {    console.log(firstName + ‘ ‘ + this.name + ‘ ‘ + lastName);}func.call(obj, ‘C‘, ‘D‘);       // C linxin D

 

對比 apply 我們可以看到區別,C 和 D 是作為單獨的參數傳給 func 函數,而不是放到數組中。

對於什麼時候該用什麼方法,其實不用糾結。如果你的參數本來就存在一個數組中,那自然就用 apply,如果參數比較散亂相互之間沒什麼關聯,就用 call。

apply 和 call 的用法1.改變 this 指向
var obj = {    name: ‘linxin‘}function func() {    console.log(this.name);}func.call(obj);       // linxin

 

我們知道,call 方法的第一個參數是作為函數內容相關的對象,這裡把 obj 作為參數傳給了 func,此時函數裡的 this 便指向了 obj 對象。此處 func 函數裡其實相當於

function func() {    console.log(obj.name);}

 

2.借用別的對象的方法

先看例子

var Person1  = function () {    this.name = ‘linxin‘;}var Person2 = function () {    this.getname = function () {        console.log(this.name);    }    Person1.call(this);}var person = new Person2();person.getname();       // linxin

 

從上面我們看到,Person2 執行個體化出來的對象 person 通過 getname 方法拿到了 Person1 中的 name。因為在 Person2 中,Person1.call(this) 的作用就是使用 Person1 對象代替 this 對象,那麼 Person2 就有了 Person1 中的所有屬性和方法了,相當於 Person2 繼承了 Person1 的屬性和方法。

3.調用函數

apply、call 方法都會使函數立即執行,因此它們也可以用來調用函數。

function func() {    console.log(‘linxin‘);}func.call();            // linxin

 

call 和 bind 的區別

在 EcmaScript5 中擴充了叫 bind 的方法,在低版本的 IE 中不相容。它和 call 很相似,接受的參數有兩部分,第一個參數是是作為函數內容相關的對象,第二部分參數是個列表,可以接受多個參數。
它們之間的區別有以下兩點。

1.bind 發傳回值是函數
var obj = {    name: ‘linxin‘}function func() {    console.log(this.name);}var func1 = func.bind(obj);func1();   
var obj = {    name: ‘linxin‘}function func() {    console.log(this.name);}var func1 = func.bind(obj);func1();   

 

                     // linxin

bind 方法不會立即執行,而是返回一個改變了上下文 this 後的函數。而原函數 func 中的 this 並沒有被改變,依舊指向全域對象 window。

2.參數的使用
function func(a, b, c) {    console.log(a, b, c);}var func1 = func.bind(null,‘linxin‘);func(‘A‘, ‘B‘, ‘C‘);            // A B Cfunc1(‘A‘, ‘B‘, ‘C‘);           // linxin A Bfunc1(‘B‘, ‘C‘);                // linxin B Cfunc.call(null, ‘linxin‘);      // linxin undefined undefined

 

call 是把第二個及以後的參數作為 func 方法的實參傳進去,而 func1 方法的實參實則是在 bind 中參數的基礎上再往後排。

在低版本瀏覽器沒有 bind 方法,我們也可以自己實現一個。

if (!Function.prototype.bind) {        Function.prototype.bind = function () {            var self = this,                        // 儲存原函數                context = [].shift.call(arguments), // 儲存需要綁定的this上下文                args = [].slice.call(arguments);    // 剩餘的參數轉為數組            return function () {                    // 返回一個新函數                self.apply(context,[].concat.call(args, [].slice.call(arguments)));            }        }    }

 

 

<轉> 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.