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

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

function
?testAguments()
{
????alert(
"
arguments.selfvalue=
"
+
arguments.selfvalue);
}
//
alert("Array.sefvalue="+new?Array().selfvalue);
//
testAguments();
/**/
/*
?*?演示函數的caller屬性.
?*?說明:(當前函數).caller:返回一個對函數的引用,該函數調用了當前函數
?
*/
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("參數1","參數2");
/**/
/*
?*?演示函數的callee屬性.
?*?說明:arguments.callee:初始值就是正被執行的?Function?對象,用于匿名函數
?
*/
function
?calleeDemo()?
{
????alert(arguments.callee);
}
//
calleeDemo();
//
(function(arg0,arg1){alert("形數數目為:"+arguments.callee.length)})();
/**/
/*
?*?演示apply,call函數的用法
?*?說明:作用都是將函數綁定到另外一個對象上去運行,兩者僅在定義參數方式有所區別:
?*???????apply(thisArg,argArray);
?*?????call(thisArg[,arg1,arg2…]?]);
?*?????即所有函數內部的this指針都會被賦值為thisArg
?
*/
?
function
?ObjectA()
{
????alert(
"
執行ObjectA()
"
);
????alert(arguments[
0
]);
 ????
this
.hit
=
function
(msg)
{alert(msg)}
????
this
.info
=
"
我來自ObjectA
"
?}
?
 ?
function
?ObjectB()
{
????alert(
"
執行ObjectB()
"
);
????
//
調用ObjectA()方法,同時ObjectA構造函數中的所有this就會被ObjectB中的this替代
????ObjectA.apply(
this
,arguments);
//
ObjectA.call(this);
????alert(
this
.info);
?}
?
//
ObjectB('參數0');
?
var
?value
=
"
global?變量
"
;
 ?
function
?Obj()
{
????
this
.value
=
"
對象!
"
;
?}
?
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
|