weidagang2046的專欄

          物格而后知致
          隨筆 - 8, 文章 - 409, 評論 - 101, 引用 - 0
          數(shù)據(jù)加載中……

          JavaScript中的arguments,callee,caller,call,appy

          < script?language = " JavaScript " >
          /*
          ?*?演示arguments的用法,如何獲取實參數(shù)和形數(shù)數(shù)
          ?
          */

          function ?argTest(a,b,c,d) {
          ????
          var ?numargs? = ?arguments.length;????? // ?獲取被傳遞參數(shù)的數(shù)值。
          ???? var ?expargs? = ?argTest.length;??????? // ?獲取期望參數(shù)的數(shù)值。
          ????alert( " 實參數(shù)目為: " + numargs)
          ????alert(
          " 形數(shù)數(shù)目為: " + expargs)

          ????alert(arguments[
          0 ])?????????
          ????alert(argTest[
          0 ])?????????? // undefined?沒有這種用法
          }

          // 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屬性.
          ?*?說明:(當前函數(shù)).caller:返回一個對函數(shù)的引用,該函數(shù)調用了當前函數(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屬性.
          ?*?說明:arguments.callee:初始值就是正被執(zhí)行的?Function?對象,用于匿名函數(shù)
          ?
          */

          function ?calleeDemo()? {
          ????alert(arguments.callee);
          }

          // calleeDemo();
          //
          (function(arg0,arg1){alert("形數(shù)數(shù)目為:"+arguments.callee.length)})();


          /*
          ?*?演示apply,call函數(shù)的用法
          ?*?說明:作用都是將函數(shù)綁定到另外一個對象上去運行,兩者僅在定義參數(shù)方式有所區(qū)別:
          ?*???????apply(thisArg,argArray);
          ?*?????call(thisArg[,arg1,arg2…]?]);
          ?*?????即所有函數(shù)內部的this指針都會被賦值為thisArg
          ?
          */


          ?
          function ?ObjectA() {
          ????alert(
          " 執(zhí)行ObjectA() " );
          ????alert(arguments[
          0 ]);
          ????
          this .hit = function (msg) {alert(msg)}
          ????
          this .info = " 我來自ObjectA "
          ?}

          ?
          ?
          function ?ObjectB() {
          ????alert(
          " 執(zhí)行ObjectB() " );
          ????
          // 調用ObjectA()方法,同時ObjectA構造函數(shù)中的所有this就會被ObjectB中的this替代
          ????ObjectA.apply( this ,arguments); // ObjectA.call(this);
          ????alert( this .info);
          ?}

          ?
          // ObjectB('參數(shù)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.aygfsteel.com/xfan/archive/2007/01/12/93504.html

          posted on 2007-01-13 08:52 weidagang2046 閱讀(443) 評論(0)  編輯  收藏 所屬分類: Javascript

          主站蜘蛛池模板: 新龙县| 临漳县| 哈尔滨市| 怀来县| 棋牌| 东平县| 大安市| 固始县| 水富县| 太保市| 九台市| 古田县| 新乡市| 龙南县| 瑞安市| 新沂市| 天祝| 柯坪县| 阳西县| 南阳市| 克山县| 兴海县| 美姑县| 民县| 盐山县| 萨迦县| 固阳县| 宕昌县| 泰州市| 新和县| 丰镇市| 长沙县| 都兰县| 巴林右旗| 新密市| 灌阳县| 陕西省| 广河县| 古浪县| 天峻县| 谢通门县|