Skip to content
Back to skills

Js State Pattern

ASecurity

> Allow an object to alter its behavior when its internal state changes

  • 20 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added September 11, 2026
developmentjavascriptgojavareact

Works with

  • cli

Security analysis

A100/100

Pro scans all 2 files and shows the line behind each finding

Scanned September 11, 2026

npx -y skills add Intense-Visions/harness-engineering --skill js-state-pattern --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Js State Pattern?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Js State Pattern
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/intense-visions-js-state-pattern/badge)](https://www.skillsdirectory.com/skills/intense-visions-js-state-pattern)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
# JS State Pattern

> Allow an object to alter its behavior when its internal state changes

## When to Use

- An object's behavior depends on its current state and must change at runtime
- You have complex conditional logic based on state (long if/else or switch chains)
- Implementing workflows, UI state machines, or protocol handlers

## Instructions

1. Define a state interface with the actions the context can perform.
2. Implement one concrete state class per state.
3. The context delegates behavior to the current state object.
4. States transition the context by calling `context.setState(newState)`.

```javascript
class TrafficLight {
  constructor() {
    this.state = new RedState(this);
  }
  setState(state) {
    this.state = state;
  }
  signal() {
    this.state.signal();
  }
}

class RedState {
  constructor(light) {
    this.light = light;
  }
  signal() {
    console.log('Red — stop');
    this.light.setState(new GreenState(this.light));
  }
}

class GreenState {
  constructor(light) {
    this.light = light;
  }
  signal() {
    console.log('Green — go');
    this.light.setState(new RedState(this.light));
  }
}

const light = new TrafficLight();
light.signal(); // Red — stop
light.signal(); // Green — go
```

## Details

The State pattern encapsulates state-dependent behavior into separate classes. The context object delegates to the current state, and states handle transitions. This eliminates complex conditionals and makes adding new states straightforward.

**Trade-offs:**

- More classes — each state is its own class, which can feel heavy for simple state machines
- Transitions are distributed across state classes — harder to see the full state diagram at a glance
- For simple boolean flags, the pattern is overkill

**When NOT to use:**

- When there are only 2-3 states with simple logic — a flag or enum is clearer
- When state transitions are rare and the behavior difference is minimal
- For UI component state — use framework state management (useState, reactive) instead

## Source

https://patterns.dev/javascript/state-pattern

## Process

1. Read the instructions and examples in this document.
2. Apply the patterns to your implementation, adapting to your specific context.
3. Verify your implementation against the details and edge cases listed above.

## Harness Integration

- **Type:** knowledge — this skill is a reference document, not a procedural workflow.
- **No tools or state** — consumed as context by other skills and agents.

## Success Criteria

- The patterns described in this document are applied correctly in the implementation.
- Edge cases and anti-patterns listed in this document are avoided.

Files in this skill

  • SKILL.md2.6 KB
  • skill.yaml666 B

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…