elevateZoom (Requires jQuery)

Facebook Messenger (Donation)

Many donors of this blog prefer to donate their money through Facebook Messenger!

Click here to go to this blog's Facebook Page!

Thank you!

Activation Code - Donation (Multiple Options)


Request Web\PHP\JavaScript\HTML Project !

Contact us if you want us to develop a Web\PHP\JavaScript\HTML project for you!

Advertise On This Blog!

If you want a Product\Service\Event to be advertised on this blog, feel free to contact us!

Ads (Reserved)

Wednesday, November 14, 2018

JavaScript - Alternative for Functions Static Variables!

This method basically allows you to use "Local" object to store functions local values and "Static" object to store functions static values!

To create the functions "Static" objects, use unique global variable names based on the function name itself such as "Function_Test_Function", "Function_ScreenGet_Function",  "Function_Calculate8776_Function", "Class_Function_Test_Class", etc, etc, etc ...!

When a function returns, javascript automatically clears all its "Local" variables values!

If you need a value to be remembered between the function calls, you need to store that value in a "Static" variable of that same function!

Unfortunately, javascript does not support functions "Static" variables! (Most languages that I know support it!)

So, an alternative is:

 - lets say your function name is "Test"

 - inside the function, create a "global" object based on your function's name, example: "Function_Test_Function"

 - inside the function, declare "Static" variable as local object by passing the reference of "Function_Test_Function" object inside it!

 - now you can store static values of your function through the local "Static" object, example: Static.x, Static.y, Static.data, etc, etc

note that:

 - using "Static.x" or "Function_Test_Function.x" is the same thing, but the former is more practical to use then the latter!

 - both "Static" or "Function_Test_Function" variables reference the same object stored in the ram memory!

 - "Static" or "Function_Test_Function" variables don't contain the object's values, they contain only a reference to an object stored in the memory!

JavaScript - Alternative for Functions Static Variables!

No comments:

Post a Comment

ADS - Multi Share Calculator