Friday, September 20, 2013

Object: SwitchState [JavaScript]

SwitchState is a JavaScript object 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
* About: This object will hold a state value of type boolean.
*   The state can be update and returned via build-in methods.

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.");


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


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


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.


// Instantiate a new SwitchState object
var enforceRequestItemComments = new SwitchState();

// Change the switch state to "off" - false.

// Display a message based on the returned switch state.
if (!enforceRequestItemComments.currentSwitchState()) {
  alert("Comments are currently off?");

// Perform an action.

Revision History

2013-09-2001.00.00Initial release.

No comments:

Post a Comment