# Control variables and method created while a program is running

**URL:** <https://discourse.processing.org/t/control-variables-and-method-created-while-a-program-is-running/47995>\
**Category:** Beginners\
**Created:** [February 28, 2026, 3:23pm UTC](https://discourse.processing.org/t/control-variables-and-method-created-while-a-program-is-running/47995 "2026-02-28T15:23:52Z")\
**Posts on this page:** 9\
**Page:** 2

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [March 9, 2026, 7:05pm UTC](https://discourse.processing.org/t/control-variables-and-method-created-while-a-program-is-running/47995/22 "2026-03-09T19:05:42Z")

</div>

> [@EricRogerGarcia](#):
>
> > [@GoToLoop](#):
> >
> > ` isBottomClass && Object.preventExtensions(instance);`
> 
> is this line the same as:
> 
> ```auto
> if (isBottomClass) Object.preventExtensions(instance);
> 
> ```

Indeed they’re the same. My version uses “short-circuit evaluation”:

> **[Short-circuit evaluation - Logical AND (&&) - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND#short-circuit_evaluation)**
>
> The logical AND expression is a short-circuit operator. As each operand is converted to a boolean, if the result of one conversion is found to be false, the AND operator stops and returns the original value of that falsy operand; it does not evaluate...

That is, `Object.preventExtensions(instance)`, which is the 2nd operand of `&&`, is only evaluated if _isBottomClass_, which is the 1st operand, happens to be “truthy”:

> **[Truthy - Glossary | MDN](https://developer.mozilla.org/en-US/docs/Glossary/Truthy)**
>
> In JavaScript, a truthy value is a value that is considered true when encountered in a Boolean context. All values are truthy unless they are defined as falsy. That is, all values are truthy except false, 0, -0, 0n, "", null, undefined, NaN, and...

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [March 9, 2026, 7:13pm UTC](https://discourse.processing.org/t/control-variables-and-method-created-while-a-program-is-running/47995/23 "2026-03-09T19:13:30Z")

</div>

> [@EricRogerGarcia](#):
>
> or is it the same as (without the `return`) :
> 
> ```auto
> static _sealInstance(instance) {
> if (instance.constructor == this) Object.preventExtensions(instance);
> }
> 
> ```

That would be a more direct comparison, skipping caching it in the local variable _isBottomClass_.

The reason I’ve chosen to have a local variable is b/c I wanted to return it from the function, flagging whether the object sealing operation was a success.

---

<div class="post-metadata">

**Author:** ![EricRogerGarcia](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ericrogergarcia/32/21029_2.png) [@EricRogerGarcia](https://discourse.processing.org/u/EricRogerGarcia)\
**Post date:** [March 9, 2026, 9:29pm UTC](https://discourse.processing.org/t/control-variables-and-method-created-while-a-program-is-running/47995/24 "2026-03-09T21:29:32Z")

</div>

Already answered!!!  
How much I appreciate this forum !!!

(Still new things to learn on Javascript! Cool!)

---

<div class="post-metadata">

**Author:** ![EricRogerGarcia](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ericrogergarcia/32/21029_2.png) [@EricRogerGarcia](https://discourse.processing.org/u/EricRogerGarcia)\
**Post date:** [March 16, 2026, 5:10pm UTC](https://discourse.processing.org/t/control-variables-and-method-created-while-a-program-is-running/47995/25 "2026-03-16T17:10:46Z")

</div>

I wonder…

is there a way to add the static \_sealInstance() method to the Object class ?

You see what I mean ! lol

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [March 16, 2026, 5:51pm UTC](https://discourse.processing.org/t/control-variables-and-method-created-while-a-program-is-running/47995/26 "2026-03-16T17:51:25Z")

</div>

- It’s bad etiquette modifying JS built-ins.
- Even if you do, you’d still need to use `extends Object` in order to inherit it.
- instead, what about having a @protected super class Sealer containing **\_sealInstance()**?

```js
#!/usr/bin/env node

// @ts-check

// https://Discourse.Processing.org/t/
// control-variables-and-method-created-while-a-program-is-running/47995/26

"use strict";

class Sealer {
  /**
   * @protected
   * @throws {TypeError}
   */
  constructor() {
    if (Sealer._sealInstance(this)) throw TypeError(
      "Sealer is an abstract utility super class and cannot be instantiated!");
  }

  /**
   * Seals `instance` if its *constructor* is the invoking class itself.
   * Prevents further property additions after constructor completes.
   *
   * @protected
   *
   * @param {Sealer} instance - object to be conditionally sealed
   *
   * @returns {boolean} whether `instance` got sealed or not 
   */
  static _sealInstance(instance) {
    const isBottomClass = instance.constructor == this;
    isBottomClass && Object.preventExtensions(instance);
    return isBottomClass;
  }
}

class A extends Sealer {
  /**
   * Creates a new being with the given name.
   *
   * @param {string} name - the name for the being
   */
  constructor(name) {
    super();

    /** the name for the being */ this.name = name;

    A._sealInstance(this); // Seal it only if this instance is exactly type A!
  }
}

class B extends A {
  /**
   * Creates a new animal with name, type and favorite food.
   *
   * @param {string} name - the name for the animal
   * @param {string} animal - kind of animal
   * @param {string} food - favorite food of the animal
   */
  constructor(name, animal, food) {
    super(name);

    // Redundant re-assignment to change prop name's IntelliSense description:
    /** the name for the animal */ this.name = this.name;

    /** kind of animal */ this.animal = animal;
    /** favorite food of the animal */ this.food = food;

    B._sealInstance(this); // Seal it only if this instance is exactly type B!
  }
}

class Pet extends B {
  /**
   * Creates a new pet with name, type and favorite food.
   *
   * @param {string} name - the name for the pet
   * @param {string} animal - kind of pet
   * @param {string} food - favorite food of the pet
   */
  constructor(name, animal, food) {
    super(name, animal, food);

    Pet._sealInstance(this); // Seal it only if this instance is type Pet!
  }

  feed() {
    const { name, animal, food } = this;
    console.info(name, "the", animal, "eats the delicious", food);
  }

  pet() {
    const { name, animal } = this;
    console.info(name, "the", animal, "loves to be petted!");
  }
}

const a = new A("Thor");

try {
  a.weapon = "hammer"; // Prop 'weapon' does not exist on type 'A' ts(2339)
} catch (err) {
  console.warn("Class A is sealed & prohibits additional properties!\n", err);
}

console.log(a.name); // "Thor"
console.table(a);

const b = new B("Pengo", "penguin", "fish");

// 1. IntelliSense recognizes props "name", "animal" & "food" as strings:
console.log('\n', b.name, b.animal, b.food, '\n'); // "Pengo" "penguin" "fish"

// 2. Sealed object safety works! Property "color" is rejected by the linter:
try {
  b.color = "gray"; // Property 'color' does not exist on type 'B' ts(2339)
} catch (err) {
  console.warn("Class B is sealed & prohibits additional properties!\n", err);
}

// 3. Runtime works:
// The object is sealed, and no class fields were generated to crash it.
console.table(b);

const { name: pet, animal, food } = b;
const c = new Pet(pet, animal, food);

try {
  c.sleep = function () { // Prop 'sleep' doesn't exist on type 'Pet' ts(2339)
    const { name, animal } = this;
    console.info(name, "the", animal, "is dreaming right now!");
  };
} catch (err) {
  console.warn("Class Pet is sealed & prohibits additional props!\n", err);
}

console.table(c);
c.feed(); c.pet();

```

---

<div class="post-metadata">

**Author:** ![EricRogerGarcia](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ericrogergarcia/32/21029_2.png) [@EricRogerGarcia](https://discourse.processing.org/u/EricRogerGarcia)\
**Post date:** [March 16, 2026, 11:07pm UTC](https://discourse.processing.org/t/control-variables-and-method-created-while-a-program-is-running/47995/27 "2026-03-16T23:07:05Z")

</div>

Yes, that’s what I thought!  
I just had the same idea!

I will also take a closer look at the try/catch and Throw Errors…

Thank you  
🙂

**P.S.** I copy/paste your post (with the link to the forum) in an annex in my project documentation !

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [March 16, 2026, 11:28pm UTC](https://discourse.processing.org/t/control-variables-and-method-created-while-a-program-is-running/47995/28 "2026-03-16T23:28:04Z")

</div>

> [@EricRogerGarcia](#):
>
> I will also take a closer look at the try/catch and Throw Errors…

The `throw` inside Sealer’s **constructor()** is so no1 would accidentally attempt to directly instantiate it thinking it does anything by itself; even though it’s only to be subclassed for it’s utility `static` method.

The 3 `try {} / catch () {}` is to prove we can’t add any additional properties to neither subclasses after their instantiation.

The directive comment `// @ts-check` enables an IDE’s TS linter to catch those 3 disallowed property additions even before running the example btW:

> **[@ts-check - Documentation - JS Projects Utilizing TypeScript](https://www.typescriptlang.org/docs/handbook/intro-to-js-ts.html#ts-check)**
>
> The type system in TypeScript has different levels of strictness when working with a codebase: | How to add type checking to JavaScript files using TypeScript

---

<div class="post-metadata">

**Author:** ![EricRogerGarcia](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ericrogergarcia/32/21029_2.png) [@EricRogerGarcia](https://discourse.processing.org/u/EricRogerGarcia)\
**Post date:** [March 30, 2026, 8:32pm UTC](https://discourse.processing.org/t/control-variables-and-method-created-while-a-program-is-running/47995/29 "2026-03-30T20:32:37Z")

</div>

A small modification to my code with some additional notes

```auto

class Sealer { // See Documentation Annex

  static sealInstance(instance) {
    
    // MUTABILITY - IMMUTABILITY
    // Nothing : anything can be modified in a class outside of the constructor
    // Object.preventExtensions(instance) : instances variables and functions cannot be added after
    // Object.seal(instance) : + instances variables and functions cannot be deleted with delete keyword after
    // Object.freeze(instance) : + instances variables values cannot be modified ("real" const !)
    
    const isBottomClass = (instance.constructor == this);
    if (isBottomClass) Object.seal(instance);
    return isBottomClass;
  }

  constructor() {
    if (Sealer.sealInstance(this)) throw TypeError(
      "Sealer is an abstract utility super class and cannot be instantiated!");
  }
}

```

---

<div class="post-metadata">

**Author:** ![EricRogerGarcia](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ericrogergarcia/32/21029_2.png) [@EricRogerGarcia](https://discourse.processing.org/u/EricRogerGarcia)\
**Post date:** [March 31, 2026, 6:19am UTC](https://discourse.processing.org/t/control-variables-and-method-created-while-a-program-is-running/47995/31 "2026-03-31T06:19:15Z")

</div>

N.B. The very principle of my project being that the game engine and graphical interface code is directly accessible by the user, the use of preventExtensions() or seal() in the Sealer class is not so important. It is above all an opportunity to learn about their existence by going through the code!

[Previous page](https://discourse.processing.org/t/control-variables-and-method-created-while-a-program-is-running/47995.md?page=1)
