<p>Say I have a programming language that can support user-defined classes (all debates over whether OOP is actually good aside) - how do I store instances of these user-defined classes when executing a program?</p>
<p>If I'm interpreting my language, what data structures do I use?</p>
<p>If I'm compiling my language, what do I write to memory and where?</p>

<compilers><interpreters><object-oriented>
How do I Implement User-Defined Classes?
CC BY-SA 4.0
2023-05-16T22:53:58.407Z
I think this could possibly be made into a good question, but it seems to be a bit broad as currently stated.
Isaiah
CC BY-SA 4.0
2023-05-16T23:04:35.137Z
Given that there are three question marks, I agree
Seggan
<h1>Functions and Prototypes</h1>
<p>This is what JavaScript does. Consider this modern TypeScript class:</p>
<pre><code>class Normal {
  x: number;

  constructor(x: number) {
    this.x = x;
  }

  getDoubleX(): number {
    return this.x * 2;
  }
}
</code></pre>
<p>This is roughly equivalent to this older JavaScript:</p>
<pre><code>function Normal(x) {
  if (!(this instanceof Normal)) {
    throw new TypeError(&quot;Class must be constructed with 'new'&quot;);
  }
  this.x = x;
  return this;
}

Normal.prototype.getDoubleX = function getDoubleX() { return this.x * 2; };
</code></pre>
<p>Then, when the <code>new</code> keyword is used, something like this happens behind the scenes:</p>
<pre><code>// var obj = new Normal(2);
var obj = Object.create(Normal.prototype);
obj = Normal.call(obj, 2);
</code></pre>
<p>This allows for some really interesting stuff at runtime, like changing what class an object belongs to or conditionally installing extension methods.</p>

CC BY-SA 4.0
2023-05-16T22:53:48.157Z
2023-05-16T22:53:48.157Z
Bbrk24
# How do I Implement User-Defined Classes?
Posted by: username_0 on 2023-05-16
Say I have a programming language that can support user-defined classes (all debates over whether OOP is actually good aside) - how do I store instances of these user-defined classes when executing a program?

If I'm interpreting my language, what data structures do I use?

If I'm compiling my language, what do I write to memory and where?
**Tags:** compilers interpreters object-oriented
> **Comments:**
> **username_1**: I think this could possibly be made into a good question, but it seems to be a bit broad as currently stated.  
> **username_2**: Given that there are three question marks, I agree  
---
## 1 answers
### Answer by username_3 on 2023-05-16. Score: 2
Functions and Prototypes
This is what JavaScript does. Consider this modern TypeScript class:
class Normal {
  x: number;

  constructor(x: number) {
    this.x = x;
  }

  getDoubleX(): number {
    return this.x * 2;
  }
}

This is roughly equivalent to this older JavaScript:
function Normal(x) {
  if (!(this instanceof Normal)) {
    throw new TypeError("Class must be constructed with 'new'");
  }
  this.x = x;
  return this;
}

Normal.prototype.getDoubleX = function getDoubleX() { return this.x * 2; };

Then, when the new keyword is used, something like this happens behind the scenes:
// var obj = new Normal(2);
var obj = Object.create(Normal.prototype);
obj = Normal.call(obj, 2);

This allows for some really interesting stuff at runtime, like changing what class an object belongs to or conditionally installing extension methods.
---
