在函數中arguments的使用

來源:互聯網
上載者:User

用於儲存和訪問函數參數的參數對象。在一個函數體內,可以使用局部參數變數來訪問其參數對象。
這些參數儲存為數組元素:第一個存取為 arguments[0],第二個存取為 arguments[1],依此類推。arguments.length 屬性工作表示傳遞給函數的參數數目。傳遞的參數數目可能與函式宣告的數目有所不同。
與以前版本的 ActionScript 不同,ActionScript 3.0 中不包含 arguments.caller 屬性。要獲得對調用當前函數的函數的引用,必須將一個引用作為參數傳遞給該函數。可從 arguments.callee 樣本中找到該技術的樣本。
ActionScript 3.0 中包括一個新的 ...(rest) 關鍵字,建議用該關鍵字替代參數類。

屬性詳細資料



callee
屬性
public var callee:Function

語言版本: 
ActionScript 3.0
運行時版本: 
AIR 1.0 Flash Player 8, Flash Lite 4

對當前正在執行的函數的引用。


樣本  ( 如何使用本樣本

)


以下代碼示範如何獲得對函數的引用,該函數調用名為 secondFunction()

的函數。firstFunction()
函數具有布爾參數 true
,以證實
secondFunction()
成功調用了
firstFunction()
,並防止各個函數因相互間調用而無限迴圈。

因為 callSecond
參數為 true
,所以
firstFunction()
調用
secondFunction()
,並將一個對自身的引用作為唯一的參數傳遞。函數
secondFunction()
接收該參數並使用名為 caller
的參數儲存它,該參數屬於
Function 資料類型。然後,使用 secondFunction()
中的 caller
參數來調用
firstFunction
函數,但這次將 callSecond
參數設定為
false

當執行返回到 firstFunction()
時,執行 trace()
語句,因為
callSecond
false

  package {    import flash.display.Sprite;        public class ArgumentsExample extends Sprite {        private var count:int = 1;                public function ArgumentsExample() {            firstFunction(true);        }        public function firstFunction(callSecond:Boolean) {            trace(count + ": firstFunction");            if(callSecond) {                secondFunction(arguments.callee);            }            else {                trace("CALLS STOPPED");            }        }        public function secondFunction(caller:Function) {            trace(count + ": secondFunction/n");            count++;            caller(false);        }            }} 



length
屬性  
public var length:Number

語言版本: 
ActionScript 3.0
運行時版本: 
AIR 1.0 Flash Player 8, Flash Lite 4

傳遞給函數的參數數目。此數目可以大於或小於函式宣告的參數數目。


樣本 如何使用本樣本


ArgumentsExample.as


以下樣本顯示各種 arguments
屬性(如 callee

length
)的用法。

package {    import flash.display.Sprite;        public class ArgumentsExample extends Sprite {        public function ArgumentsExample() {                println("Hello World");        }                public function println(str:String):void {            trace(arguments.callee == this.println); // true            trace(arguments.length);                 // 1            trace(arguments[0]);                     // Hello World            trace(str);                                // Hello World        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.