|
<
script?language
=
"
JavaScript
"
>
/**/
/*
?*?演示arguments的用法,如何獲取實(shí)參數(shù)和形數(shù)數(shù)
?
*/
function
?argTest(a,b,c,d)
{
????
var
?numargs?
=
?arguments.length;?????
//
?獲取被傳遞參數(shù)的數(shù)值。
????
var
?expargs?
=
?argTest.length;???????
//
?獲取期望參數(shù)的數(shù)值。
????alert(
"
實(shí)參數(shù)目為:
"
+
numargs)
????alert(
"
形數(shù)數(shù)目為:
"
+
expargs)

????alert(arguments[
0
])?????????
????alert(argTest[
0
])??????????
//
undefined?沒(méi)有這種用法
}
//
argTest(1,2)
//
argTest(1,2,3,4,5)
/**/
/*
?*??arguments不是數(shù)組(Array類)
?
*/
Array.prototype.selfvalue?
=
?
1
;

function
?testAguments()
{
????alert(
"
arguments.selfvalue=
"
+
arguments.selfvalue);
}
//
alert("Array.sefvalue="+new?Array().selfvalue);
//
testAguments();
/**/
/*
?*?演示函數(shù)的caller屬性.
?*?說(shuō)明:(當(dāng)前函數(shù)).caller:返回一個(gè)對(duì)函數(shù)的引用,該函數(shù)調(diào)用了當(dāng)前函數(shù)
?
*/
function
?callerDemo()?
{
 ????
if
?(callerDemo.caller)?
{
????????
var
?a
=
?callerDemo.caller.arguments[
0
];
????????alert(a);
 ????}
?
else
?
{
????????alert(
"
this?is?a?top?function
"
);
????}
}
function
?handleCaller()?
{
????callerDemo();
}
//
callerDemo();
//
handleCaller("參數(shù)1","參數(shù)2");
/**/
/*
?*?演示函數(shù)的callee屬性.
?*?說(shuō)明:arguments.callee:初始值就是正被執(zhí)行的?Function?對(duì)象,用于匿名函數(shù)
?
*/
function
?calleeDemo()?
{
????alert(arguments.callee);
}
//
calleeDemo();
//
(function(arg0,arg1){alert("形數(shù)數(shù)目為:"+arguments.callee.length)})();
/**/
/*
?*?演示apply,call函數(shù)的用法
?*?說(shuō)明:作用都是將函數(shù)綁定到另外一個(gè)對(duì)象上去運(yùn)行,兩者僅在定義參數(shù)方式有所區(qū)別:
?*???????apply(thisArg,argArray);
?*?????call(thisArg[,arg1,arg2…]?]);
?*?????即所有函數(shù)內(nèi)部的this指針都會(huì)被賦值為thisArg
?
*/
?
function
?ObjectA()
{
????alert(
"
執(zhí)行ObjectA()
"
);
????alert(arguments[
0
]);
 ????
this
.hit
=
function
(msg)
{alert(msg)}
????
this
.info
=
"
我來(lái)自O(shè)bjectA
"
?}
?
 ?
function
?ObjectB()
{
????alert(
"
執(zhí)行ObjectB()
"
);
????
//
調(diào)用ObjectA()方法,同時(shí)ObjectA構(gòu)造函數(shù)中的所有this就會(huì)被ObjectB中的this替代
????ObjectA.apply(
this
,arguments);
//
ObjectA.call(this);
????alert(
this
.info);
?}
?
//
ObjectB('參數(shù)0');
?
var
?value
=
"
global?變量
"
;
 ?
function
?Obj()
{
????
this
.value
=
"
對(duì)象!
"
;
?}
?
function
?Fun1()
{
????alert(
this
.value);
?}
?
//
Fun1();
?
//
Fun1.apply(window);?
?
//
Fun1.apply(new?Obj());?
</
script
>
from: http://www.tkk7.com/xfan/archive/2007/01/12/93504.html
|