Titan Fusion

Alexandar Tzanov's Personal Blog

Object: SwitchState


SwitchState is a JavaScript object type which can be used to control when to work with another object, display a message, or when to validate certain field of a web form.

Source Code

  1. /* Name: SwitchState
  2.  * Developer: Alexandar Tzanov
  3.  * Version: 1.0.0
  4.  * Revised: 2013-09-20
  5.  */
  6.  
  7. function SwitchState()
  8. {
  9.     // Variables
  10.     var switchState = true;
  11.  
  12.     // Methods
  13.     // Return the state of the switch to an external call.
  14.     this.currentSwitchState = function()
  15.     {
  16.         return switchState;
  17.     }
  18.  
  19.     // Update the switch state.
  20.     this.updateSwitchState = function(newState)
  21.     {
  22.         if (typeof newState === "undefined" || newState === '')
  23.         {
  24.             newState = true;
  25.         }
  26.         switchState = newState;
  27.     }
  28.  
  29.     // Perform an action based on the switch state condition.
  30.     this.doThis = function()
  31.     {
  32.         if (switchState)
  33.         {
  34.             alert("Current status is set to true.");
  35.         }
  36.         if (!switchState)
  37.         {
  38.             alert("Current status is set to false.");
  39.         }
  40.     }
  41. }

Usage

Object.updateSwitchState( boolean false);
 
boolean Object.currentSwitchState();
 
[can vary on developer setup] Object.doThis();

Parameters

Name Definition Default Value
newState Sets the condition of the switchState variable. true

Return

A SwitchState object will return the current state of the switch (boolean – true / false) when currentSwitchState is called.

When called upon the doThis method will perform an action. You can modify that to an alert or some other action, e.g. innerHTML.

Examples

// Instantiate a new SwitchState object
var enforceRequestItemComments = new SwitchState();
 
// Change the switch state to "off" - false.
enforceRequestItemComments.updateSwitchState(false);
 
// Display a message based on the returned switch state.
if (!enforceRequestItemComments.currentSwitchState())
{
    alert("Comments are currently off?");
}
 
// Perform an action.
enforceRequestItemComments.doThis();

Revision History

Date Version Changes
2013-09-20 01.00.00 Initial release.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.