javascript中call apply 與 bind方法詳解
在JavaScript中,call、apply和bind是Function對象自帶的三個方法,本文將通過幾個場景的應(yīng)用,來詳細(xì)理解三個方法。
call()
call() 方法在使用一個指定的this值和若干個指定的參數(shù)值的前提下調(diào)用某個函數(shù)或方法。
當(dāng)調(diào)用一個函數(shù)時,可以賦值一個不同的 this 對象。this 引用當(dāng)前對象,即 call 方法的第一個參數(shù)。
通過 call 方法,你可以在一個對象上借用另一個對象上的方法,比如Object.prototype.toString.call([]),就是一個Array對象借用了Object對象上的方法。
語法 fun.call(thisArg[, arg1[, arg2[, ...]]])
thisArg
在fun函數(shù)運行時指定的this值。需要注意的是下面幾種情況
(1)不傳,或者傳null,undefined, 函數(shù)中的this指向window對象
(2)傳遞另一個函數(shù)的函數(shù)名,函數(shù)中的this指向這個函數(shù)的引用,并不一定是該函數(shù)執(zhí)行時真正的this值
(3)值為原始值(數(shù)字,字符串,布爾值)的this會指向該原始值的自動包裝對象,如 String、Number、Boolean
(4)傳遞一個對象,函數(shù)中的this指向這個對象
arg1, arg2, ...
指定的參數(shù)列表。
例子
初級應(yīng)用例子
function a(){ //輸出函數(shù)a中的this對象 console.log(this); } //定義函數(shù)b function b(){} var obj = {name:'這是一個屌絲'}; //定義對象obj a.call(); //window a.call(null); //window a.call(undefined);//window a.call(1); //Number a.call(''); //String a.call(true); //Boolean a.call(b);// function b(){} a.call(obj); //Object
使用call方法調(diào)用匿名函數(shù)并且指定上下文的this
在下面的例子中,當(dāng)調(diào)用 greet 方法的時候,該方法的 this 值會綁定到 i對象。
function greet() { var reply = [this.person, '是一個輕量的', this.role].join(' '); console.log(reply); } var i = {function greet() { var reply = [this.person, '是一個輕量的', this.role].join(' '); console.log(reply); } var i = { person: 'JSLite.io', role: 'Javascript 庫。' }; greet.call(i); // JSLite.io 是一個輕量的 Javascript 庫。 person: 'JSLite.io', role: 'Javascript 庫。' }; greet.call(i); // JSLite.io 是一個輕量的 Javascript 庫。
使用call方法調(diào)用匿名函數(shù)
在下例中的for循環(huán)體內(nèi),我們創(chuàng)建了一個匿名函數(shù),然后通過調(diào)用該函數(shù)的call方法,將每個數(shù)組元素作為指定的this值執(zhí)行了那個匿名函數(shù)。這個匿名函數(shù)的主要目的是給每個數(shù)組元素對象添加一個print方法,這個print方法可以打印出各元素在數(shù)組中的正確索引號。當(dāng)然,這里不是必須得讓數(shù)組元素作為this值傳入那個匿名函數(shù)(普通參數(shù)就可以),目的是為了演示call的用法。
var animals = [ {species: 'Lion', name: 'King'}, {species: 'Whale', name: 'Fail'} ]; for (var i = 0; i < animals.length; i++) { (function (i) { this.print = function () { console.log('#' + i + ' ' + this.species + ': ' + this.name); } this.print(); }).call(animals[i], i); } //#0 Lion: King //#1 Whale: Fail
使用call方法調(diào)用函數(shù)傳參數(shù)
var a = { name:'JSLite.io', //定義a的屬性 say:function(){ //定義a的方法 console.log("Hi,I'm function a!"); } }; function b(name){ console.log("Post params: "+ name); console.log("I'm "+ this.name); this.say(); } b.call(a,'test'); //Post params: test //I'm onepixel //I'm function a!
apply()
語法與 call() 方法的語法幾乎完全相同,唯一的區(qū)別在于,apply的第二個參數(shù)必須是一個包含多個參數(shù)的數(shù)組(或類數(shù)組對象)。apply的這個特性很重要,
在調(diào)用一個存在的函數(shù)時,你可以為其指定一個 this 對象。 this 指當(dāng)前對象,也就是正在調(diào)用這個函數(shù)的對象。 使用 apply, 你可以只寫一次這個方法然后在另一個對象中繼承它,而不用在新對象中重復(fù)寫該方法。
語法:fun.apply(thisArg[, argsArray])
注意: 需要注意:Chrome 14 以及 Internet Explorer 9 仍然不接受類數(shù)組對象。如果傳入類數(shù)組對象,它們會拋出異常。
參數(shù)
thisArg
同上call 的thisArg參數(shù)。
argsArray
一個數(shù)組或者類數(shù)組對象,其中的數(shù)組元素將作為單獨的參數(shù)傳給 fun 函數(shù)。如果該參數(shù)的值為null 或 undefined,則表示不需要傳入任何參數(shù)。從ECMAScript 5 開始可以使用類數(shù)組對象。
例子
function jsy(x,y,z){ console.log(x,y,z); } jsy.apply(null,[1,2,3]); // 1 2 3
使用apply來鏈接構(gòu)造器的例子
你可以使用apply來給一個對象鏈接構(gòu)造器,類似于Java. 在接下來的例子中我們會創(chuàng)建一個叫做construct的全局的Function函數(shù),來使你能夠在構(gòu)造器中使用一個類數(shù)組對象而非參數(shù)列表。
Function.prototype.construct = function(aArgs) { var fConstructor = this, fNewConstr = function() { fConstructor.apply(this, aArgs); }; fNewConstr.prototype = fConstructor.prototype; return new fNewConstr(); }; function MyConstructor () { for (var nProp = 0; nProp < arguments.length; nProp++) { console.log(arguments,this) this["property" + nProp] = arguments[nProp]; } } var myArray = [4, "Hello world!", false]; var myInstance = MyConstructor.construct(myArray); console.log(myInstance.property1); // logs "Hello world!" console.log(myInstance instanceof MyConstructor); // logs "true" console.log(myInstance.constructor); // logs "MyConstructor"
使用apply和內(nèi)置函數(shù)
聰明的apply用法允許你在某些本來需要寫成遍歷數(shù)組變量的任務(wù)中使用內(nèi)建的函數(shù)。在接下里的例子中我們會使用Math.max/Math.min來找出一個數(shù)組中的最大/最小值。
//里面有最大最小數(shù)字值的一個數(shù)組對象 var numbers = [5, 6, 2, 3, 7]; /* 使用 Math.min/Math.max 在 apply 中應(yīng)用 */ var max = Math.max.apply(null, numbers); // 一般情況是用 Math.max(5, 6, ..) 或者 Math.max(numbers[0], ...) 來找最大值 var min = Math.min.apply(null, numbers); //通常情況我們會這樣來找到數(shù)字的最大或者最小值 //比對上面的栗子,是不是下面的看起來沒有上面的舒服呢? max = -Infinity, min = +Infinity; for (var i = 0; i < numbers.length; i++) { if (numbers[i] > max) max = numbers[i]; if (numbers[i] < min) min = numbers[i]; }
參數(shù)數(shù)組切塊后循環(huán)傳入
function minOfArray(arr) { var min = Infinity; var QUANTUM = 32768; for (var i = 0, len = arr.length; i < len; i += QUANTUM) { var submin = Math.min.apply(null, arr.slice(i, Math.min(i + QUANTUM, len))); console.log(submin, min) min = Math.min(submin, min); } return min; } var min = minOfArray([5, 6, 2, 3, 7]);
bind
bind() 函數(shù)會創(chuàng)建一個新函數(shù)(稱為綁定函數(shù))
bind是ES5新增的一個方法
傳參和call或apply類似
不會執(zhí)行對應(yīng)的函數(shù),call或apply會自動執(zhí)行對應(yīng)的函數(shù)
返回對函數(shù)的引用
語法 fun.bind(thisArg[, arg1[, arg2[, ...]]])
下面例子:當(dāng)點擊網(wǎng)頁時,EventClick被觸發(fā)執(zhí)行,輸出JSLite.io p1 p2, 說明EventClick中的this被bind改變成了obj對象。如果你將EventClick.bind(obj,'p1','p2') 變成 EventClick.call(obj,'p1','p2') 的話,頁面會直接輸出 JSLite.io p1 p2
var obj = {name:'JSLite.io'}; /** * 給document添加click事件監(jiān)聽,并綁定EventClick函數(shù) * 通過bind方法設(shè)置EventClick的this為obj,并傳遞參數(shù)p1,p2 */ document.addEventListener('click',EventClick.bind(obj,'p1','p2'),false); //當(dāng)點擊網(wǎng)頁時觸發(fā)并執(zhí)行 function EventClick(a,b){ console.log( this.name, //JSLite.io a, //p1 b //p2 ) } // JSLite.io p1 p2
兼容
if (!Function.prototype.bind) { Function.prototype.bind = function (oThis) { if (typeof this !== "function") { // closest thing possible to the ECMAScript 5 // internal IsCallable function throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable"); } var aArgs = Array.prototype.slice.call(arguments, 1), fToBind = this, // this在這里指向的是目標(biāo)函數(shù) fNOP = function () {}, fBound = function () { return fToBind.apply(this instanceof fNOP ? this //此時的this就是new出的obj : oThis || this,//如果傳遞的oThis無效,就將fBound的調(diào)用者作為this //將通過bind傳遞的參數(shù)和調(diào)用時傳遞的參數(shù)進行合并,并作為最終的參數(shù)傳遞 aArgs.concat(Array.prototype.slice.call(arguments))); }; fNOP.prototype = this.prototype; //將目標(biāo)函數(shù)的原型對象拷貝到新函數(shù)中,因為目標(biāo)函數(shù)有可能被當(dāng)作構(gòu)造函數(shù)使用 fBound.prototype = new fNOP(); //返回fBond的引用,由外部按需調(diào)用 return fBound; }; }
應(yīng)用場景:繼承
function Animal(name,weight){ this.name = name; this.weight = weight; } function Cat(){ // 在call中將this作為thisArgs參數(shù)傳遞 // Animal方法中的this就指向了Cat中的this // 所以Animal中的this指向的就是cat對象 // 在Animal中定義了name和weight屬性,就相當(dāng)于在cat中定義了這些屬性 // cat對象便擁有了Animal中定義的屬性,從而達到了繼承的目的 Animal.call(this,'cat','50'); //Animal.apply(this,['cat','50']); this.say = function(){ console.log("I am " + this.name+",my weight is " + this.weight); } } //當(dāng)通過new運算符產(chǎn)生了cat時,Cat中的this就指向了cat對象 var cat = new Cat(); cat.say(); //輸出=> I am cat,my weight is 50
原型擴展
在原型函數(shù)上擴展和自定義方法,從而不污染原生函數(shù)。例如:我們在 Array 上擴展一個 forEach
function test(){ // 檢測arguments是否為Array的實例 console.log( arguments instanceof Array, //false Array.isArray(arguments) //false ); // 判斷arguments是否有forEach方法 console.log(arguments.forEach); // undefined // 將數(shù)組中的forEach應(yīng)用到arguments上 Array.prototype.forEach.call(arguments,function(item){ console.log(item); // 1 2 3 4 }); } test(1,2,3,4);
- 再談JavaScript中bind、call、apply三個方法的區(qū)別與使用方式
- 使用JS簡單實現(xiàn)apply、call和bind方法的實例代碼
- JS 函數(shù)的 call、apply 及 bind 超詳細(xì)方法
- JavaScript函數(shù)之call、apply以及bind方法案例詳解
- Javascript中call,apply,bind方法的詳解與總結(jié)
- 實例講解JavaScript中call、apply、bind方法的異同
- 深入理解JavaScript中的call、apply、bind方法的區(qū)別
- JS中改變this指向的方法(call和apply、bind)
- js apply/call/caller/callee/bind使用方法與區(qū)別分析
- JavaScript手寫call,apply,bind方法
相關(guān)文章
簡介JavaScript中Math.cos()余弦方法的使用
這篇文章主要介紹了簡介JavaScript中Math.cos()余弦方法的使用,是JS入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-06-06JS失效 提示HTML1114: (UNICODE 字節(jié)順序標(biāo)記)的代碼頁 utf-8 覆蓋(META 標(biāo)記)的沖突的代
今天使用F12調(diào)試的時候提示HTML1114: (UNICODE 字節(jié)順序標(biāo)記)的代碼頁 utf-8 覆蓋(META 標(biāo)記)的沖突的代碼頁 utf-8,需要的朋友可以參考下2017-06-06ASP小貼士/ASP Tips javascript tips可以當(dāng)桌面
今天看到《ASP小貼士/ASP Tips》 我也去把JavaScript的tips 下下來了。 看看是A4的。 自己把他改成1024 * 768 剛好可以用來做桌面2009-12-12淺談javascript中關(guān)于日期和時間的基礎(chǔ)知識
下面小編就為大家?guī)硪黄獪\談javascript中關(guān)于日期和時間的基礎(chǔ)知識。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-07-07javascript中聲明函數(shù)的方法及調(diào)用函數(shù)的返回值
這篇文章主要介紹了javascript中聲明函數(shù)的方法及調(diào)用函數(shù)時的返回值,示例如下,不了解的方法可以參考下2014-07-07JavaScript對內(nèi)存分配及管理機制詳細(xì)解析
本文主要講述了JavaScript的垃圾回收原理和具體的過程。需要的朋友可以過來參考下,希望對大家有所幫助2013-11-11