原文:(http://www.cnblogs.com/Ghost-Draw-Sign/articles/1530108.html)
參考:http://www.cnblogs.com/libin-1/p/5823025.html
在提到上述的概念之前,首先想說說javascript中函數的隱含參數:arguments
Arguments##
該對象代表正在執行的函數和調用它的函數的參數。
[function.]arguments[n]
參數:
function :選項。當前正在執行的 Function 對象的名字。
n :選項, 要傳遞給 Function 對象的從0開始的參數值索引。 說明Arguments 是進行函數調用時,除了指定的參數外,還另外創建的一個隱藏對象。
Arguments是一個類似數組但不是數組的對象。
說它類似數組是因為其具有數組一樣的訪問性質及方式,可以由arguments[n]來訪問對應的單個參數的值,并擁有數組長度屬性length。還有就是arguments對象存儲的是實際傳遞給函數的參數,而不局限于函數聲明所定義的參數列表,而且不能顯式創建 arguments 對象。arguments 對象只有函數開始時才可用。
function ArgTest(a, b){
var i, s = "The ArgTest function expected ";
var numargs = arguments.length; // 實際被傳遞參數的數值。
var expargs = ArgTest.length; // 期望參數的數值。
if (expargs < 2)
s += expargs + " argument. ";
else
s += expargs + " arguments. ";
if (numargs < 2)
s += numargs + " was passed.";
else
s += numargs + " were passed.";
s += "\n\n"
for (i =0 ; i < numargs; i++){ // 獲取參數內容。
s += " Arg " + i + " = " + arguments[i] + "\n";
}
return(s); // 返回參數列表。
}
在此添加了一個說明arguments不是數組(Array類)的代碼:
Array.prototype.selfvalue = 1;
alert(new Array().selfvalue);
function testAguments(){
alert(arguments.selfvalue);
}
運行代碼你會發現第一個alert顯示1,這表示數組對象擁有selfvalue屬性,值為1,而當你調用函數testAguments時,你會發現顯示的是“undefined”,說明了不是arguments的屬性,即arguments并不是一個數組對象。
caller##
返回一個對函數的引用,該函數調用了當前函數。
functionName.caller
返回一個對函數的引用,該函數調用了當前函數。
說明
對于函數來說,caller 屬性只有在函數執行時才有定義。
如果函數是由頂層調用的,那么 caller 包含的就是 null 。
如果在字符串上下文中使用 caller 屬性,那么結果和 functionName.toString 一樣,也就是說,顯示的是函數的反編譯文本。
// caller demo {
function callerDemo() {
if (callerDemo.caller) {
var a= callerDemo.caller.toString();
alert(a);
} else {
alert("this is a top function");
}
}
function handleCaller() {
callerDemo(); //"function handleCaller() { callerDemo();}"
}
callee
返回正被執行的 Function 對象,也就是所指定的 Function 對象的正文。
ES5 提示: 在嚴格模式下,arguments.callee 會報錯 TypeError,因為它已經被廢除了。
[function.]arguments.callee
說明
function 參數是當前正在執行的 Function 對象的名稱。
callee 屬性的初始值就是正被執行的 Function 對象。
function 參數是當前正在執行的 Function 對象的名稱。
callee 屬性是 arguments 對象的一個成員,它表示對函數對象本身的引用,這有利于匿名函數的遞歸或者保證函數的封裝性
例如下邊示例的遞歸計算1到n的自然數之和。而該屬性僅當相關函數正在執行時才可用。還有需要注意的是callee擁有length屬性,這個屬性有時候用于驗證還是比較好的。
arguments.length是實參長度,arguments.callee.length是形參長度.由此可以判斷調用時形參長度是否和實參長度一致。
//callee可以打印其本身
function calleeDemo() {
alert(arguments.callee);
}
calleeDemo(); //返回函數function calleeDemo() { alert(arguments.callee);}
//用于驗證參數
function calleeLengthDemo(arg1, arg2) {
if (arguments.length==arguments.callee.length) {
window.alert("驗證形參和實參長度正確!");
return;
} else {
alert("實參長度:" +arguments.length);
alert("形參長度: " +arguments.callee.length);
}
}
//遞歸計算
var sum = function(n){
if (n <= 0)
return 1;
else
return n +arguments.callee(n - 1)
}
比較一般的遞歸函數:
var sum = function(n){
if (1==n) return 1;
else return n + sum (n-1);
調用時:alert(sum(100));
其中函數內部包含了對sum自身的引用,函數名僅僅是一個變量名,在函數內部調用sum即相當于調用一個全局變量,不能很好的體現出是調用自身,這時使用callee會是一個比較好的方法。
apply and call##
它們的作用都是將函數綁定到另外一個對象上去運行,兩者僅在定義參數方式有所區別: apply( thisArg , argArray ); call( thisArg[,arg1,arg2…] ] );
即所有**函數內部的this指針都會被賦值為 thisArg**,這可實現**將函數作為另外一個對象的方法運行**的目的
apply的說明###
如果 argArray 不是一個有效的數組或者不是 arguments 對象,那么將導致一個 `TypeError`。
第一個參數是必須的,可以是null,undefined,this,但是不能為空。設置為null,undefined,this則等同于指定全局對象。。
call的說明
call 方法可將一個函數的對象上下文從初始的上下文改變為由 thisArg指定的新對象。 **如果沒有提供 thisArg參數,那么 Global 對象被用作 thisArg**
相關技巧:
應用call和apply還有一個技巧在里面,就是用 call 和 apply 應用另一個函數(類)以后,當前的函數(類)就具備了另一個函數(類)的方法或者是屬性,這也可以稱之為 繼承 。看下面示例:
// 繼承的演示
function base() {
this.member = " dnnsun_Member";
this.method = function() {
window.alert(this.member);
}
}
function extend() {
base.call(this);
window.alert(member);
window.alert(this.method);
}
上面的例子可以看出,通過call之后,extend可以繼承到base的方法和屬性。
多重繼承:
function Class10()
{
this.showSub = function(a,b)
{
alert(a-b);
}
}
function Class11()
{
this.showAdd = function(a,b)
{
alert(a+b);
}
}
function Class2()
{
Class10.call(this);
Class11.call(this);
}
使用兩個 call 就實現多重繼承了當然,js的繼承還有其他方法,例如使用原型鏈
apply方法有以下應用:
- 找出數組中的最大數
1 var a = [2, 4, 5, 7, 8, 10];
2
3 console.log(Math.max.apply(null, a)); //10
4 console.log(Math.max.call(null,2, 4, 5, 7, 8, 10)); //10
Javascript中是沒有提供找出數組中最大值的方法的,結合使用繼承自Function.prototype的apply和Math.max方法,就可以返回數組的最大值。
- 將數組的空元素變為undefined
通過apply方法,利用Array構造函數將數組的空元素變成undefined。
console.log(Array.apply(null, [1, , 3])); // [1, undefined, 3]
空元素與undefined的差別在于,數組的forEach方法會跳過空元素,但是不會跳過undefined和null。因此,遍歷內部元素的時候,會得到不同的結果。
var a = [1, , 3];
a.forEach(function(index) {
console.log(index); //1,3 ,跳過了空元素。
})
Array.apply(null,a).forEach(function(index){
console.log(index); ////1,undefined,3 ,將空元素設置為undefined
})
- 轉換類似數組的對象
另外,利用數組對象的slice方法,可以將一個類似數組的對象(比如arguments對象)轉為真正的數組。當然,slice方法的一個重要應用,就是將類似數組的對象轉為真正的數組。call和apply都可以實現該應用。
1 console.log(Array.prototype.slice.apply({0:1,length:1})); //[1]
2 console.log(Array.prototype.slice.call({0:1,length:1})); //[1]
3 console.log(Array.prototype.slice.apply({0:1,length:2})); //[1,undefined]
4 console.log(Array.prototype.slice.call({0:1,length:2})); //[1,undefined]
1 function keith(a,b,c){
2 return arguments; //類數組對象
3 }
4
5 console.log(Array.prototype.slice.call(keith(2,3,4))); //[2,3,4]
上面代碼的call,apply方法的參數都是對象,但是返回結果都是數組,這就起到了將對象轉成數組的目的。從上面代碼可以看到,這個方法起作用的前提是,被處理的對象必須有length屬性,以及相對應的數字鍵。
順便提一下,在javascript框架prototype里就使用apply來創建一個定義類的模式
其實現代碼如下:
var Class = {
create: function() {
return function() {
this.initialize.apply(this, arguments);
}
}
}
解析:從代碼看,該對象僅包含一個方法:Create,其返回一個函數,即類。但這也同時是類的構造函數,其中調用initialize,而這個方法是在類創建時定義的初始化函數。通過如此途徑,就可以實現prototype中的類創建模式
示例:
var vehicle=Class.create();
vehicle.prototype={
initialize:function(type){
this.type=type;
}
showSelf:function(){
alert("this vehicle is "+ this.type);
}
}
var moto=new vehicle("Moto");
moto.showSelf();
bind##
bind方法用于指定函數內部的this指向(執行時所在的作用域),然后返回一個新函數。bind方法并非立即執行一個函數。
var keith = {
a: 1,
count: function() {
console.log(this.a++);
}
};
var f = keith.count;
f(); //NaN
上面代碼中,如果把count方法賦值給f變量,那么this對象指向不再是keith對象了,而是window對象。而window.a默認為undefined,進行遞增運算之后undefined++就等于NaN。
為了解決這個問題,可以使用bind方法,將keith對象里的this綁定到keith對象上,或者是直接調用。
1 var f = keith.count.bind(keith);
2 f(); //1
3 f(); //2
4 f(); //3
當然,this也可以綁定到其他對象上。
var obj = {
a: 100
};
var f = keith.count.bind(obj);
f(); //100
f(); //101
f(); //102
function keith(a, b) {
return a + b;
}
console.log(keith.apply(null,[1,4])); //5
console.log(keith.call(null,1,4)); //5
console.log(keith.bind(null, 1, 4)); //keith()
console.log(keith.bind(null, 1, 4)()); //5
bind方法與apply/call方法也非常類似,相當于稍微再封裝了一下,仍以上述DEMO作為案例:
function Point(x, y){
this.x = x;
this.y = y;
}
Point.prototype.move = function(x, y) {
this.x += x;
this.y += y;
}
var p = new Point(0,0);
var circle = {x:1, y:1, r:1};
// p.move.call(circle, 2, 1);
// p.move.apply(circle, [2, 1]);
var circleMove = p.move.bind(circle, 2, 1); //此時并不執行move方法
circleMove(); //此時才執行
從上面這段DEMO可以看出,bind方法其實是給apply/call方法緩了一下,也可以說是封裝了一下方便后續調用,其實質上相當于下面的這段代碼:
function circleMove() {
p.move.call(circle, 2, 1);
}
circleMove();
bind方法兼容性適應###
在EcmaScript5中擴展了叫bind的方法(IE6,7,8不支持),使用方法如下
if (!Function.prototype.bind) {
Function.prototype.bind = function(obj) {
var _self = this
,args = arguments;
return function() {
_self.apply(obj, Array.prototype.slice.call(args, 1));
}
}
}
call,apply,bind這三個方法其實都是繼承自Function.prototype
可以看出call,apply,bind三者的區別:
①bind的返回值是函數。
②call和apply方法都是在調用之后立即執行的。而bind調用之后是返回原函數,需要再調用一次才行,有點像閉包的味道。