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

/* Name: SwitchState
 * Developer: Alexandar Tzanov
 * Version: 1.0.0
 * Revised: 2013-09-20
 */
 
function SwitchState()
{
    // Variables
    var switchState = true;
 
    // Methods
    // Return the state of the switch to an external call.
    this.currentSwitchState = function()
    {
        return switchState;
    }
 
    // Update the switch state.
    this.updateSwitchState = function(newState)
    {
        if (typeof newState === "undefined" || newState === '')
        {
            newState = true;
        }
        switchState = newState;
    }
 
    // Perform an action based on the switch state condition.
    this.doThis = function()
    {
        if (switchState)
        {
            alert("Current status is set to true.");
        }
        if (!switchState)
        {
            alert("Current status is set to false.");
        }
    }
}

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.