As3 tips-use internal namespace to dynamically access regional variables in scope chain

Source: Internet
Author: User

Java
Script is a region variable that can use eval to dynamically access scope chain.
As follows:

  1. VaR OBJ = {};
  2. (Function (OBJ ){
  3. VaR var3 = "Varia
    Ble 3 ";
  4. (Function (OBJ ){
  5. VaR var2 = "variable 2 ";
  6. (Function (OBJ ){
  7. VaR var1 = "variable 1 ";
  8. OBJ. getvar = function (varname ){
  9. Return eval (varname );
  10. }
  11. OBJ. setvar = function (varname, value ){
  12. Return eval (varname + "= value ");
  13. }
  14. }) (OBJ );
  15. }) (OBJ );
  16. }) (OBJ );

  17. Console. Log (obj. getvar ("var2"); // variable 2
  18. OBJ. setvar ("var2" ,{ ID: 123 })
  19. Console. Log (obj. getvar ("var2"). Id); // 123

Copy code

Eval function removed after the revised version of ActionScript 3.0
Is it because of this that the access zone variable cannot be dynamically evaluated?

I thought so before.
However, I recently saw articles related to scope chain.
Combined with the as3 namespace syntax learned in the past
I suddenly had other ideas, so I started to test it ~

After the test, a new discovery was made.
Originally, we could use internal namespace to dynamically access the regional variables of scope chain! 』

The namespace built in as3 syntax mainly includes as3, public, protected, private, and internal...
ActionScript language and syntax-packages and namespaces

ActionScript language and syntax-kit and namespace

The key is to use the internal namespace.
File
You can access the namespace at the package level.
But it can also be used to access the regional variables of the scope chain.

The following is a simple demo:

  1. Package {
  2. /**
  3. * Dynamic access to scope variable through the internal namespace
  4. */
  5. Import flash
    . Display. Sprite;

  6. [SWF (width = "300", Height = "200")]
  7. Public class scopetest01 extends sprite {

  8. Public Function scopetest01 (){

  9. VaR OBJ: Object = {};
  10. (Function (OBJ: Object): void {
  11. VaR var1: String = "variable 1 ";
  12. VaR var2: String = "variable 2 ";

  13. OBJ. getvar = function (Name: string ):*{
  14. Return internal: [name];
  15. };
  16. OBJ. setvar = function (Name: String, value :*):*{
  17. Return internal: [name] = value;
  18. };
  19. }) (OBJ );

  20. Trace (obj. getvar ("var1"); // variable 1
  21. OBJ. setvar ("var1", "hello ");
  22. Trace (obj. getvar ("var1"); // hello
  23. }
  24. }
  25. }
  26. // Ticore's blog-http://ticore.blogspot.com/

Copy code

Amazing!
After Eval is removed, as3 uses the dynamic evaluation capability of the loss and internal namespace to cleverly retrieve the loss.

As3 uses with and proxy to selectively replace scope Variables

ActionScript "with" Block scope Masking Effect

ActionScript 3.0-function Closure

As 3.0 simulates multi-layer block scope using anonymous Functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.