actionscript 3 - Using local variables in global scope after exiting function -
i understand as3 works in following way
var str1:string = "global"; function scopetest () { var str1:string = "local"; trace(str1); // local } scopetest(); trace(str1); // global
how can make work this? see last line
var str1:string = "global"; function scopetest () { var str1:string = "local"; trace(str1); // local } scopetest(); trace(str1); // local
var str1:string = "global"; function scopetest () { //use global variable here str1 = "local"; trace(str1); // local } scopetest(); trace(str1); // local
or use this
var str1:string = "global"; function scopetest () { //use "this" keyword this.str1 = "local"; var str1:string = "local"; trace(str1); // local } scopetest(); trace(str1); // local
full sample code:
<?xml version="1.0" encoding="utf-8"?> <s:application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minwidth="955" minheight="600" creationcomplete="init(event)"> <fx:script> <![cdata[ import mx.events.flexevent; protected function init(event:flexevent):void { scopetest(); trace(str1); // local } public var str1:string = "global"; public function scopetest():void { //use "this" keyword this.str1 = "local"; var str1:string = "local"; trace(str1); // local } ]]> </fx:script> <fx:declarations> <!-- place non-visual elements (e.g., services, value objects) here --> </fx:declarations> </s:application>
Comments
Post a Comment