Post B2mMjBbrsa4FHb1d68 by Professor_Stevens@mastodon.gamedev.place
(DIR) More posts by Professor_Stevens@mastodon.gamedev.place
(DIR) Post #B2mKRJS9mNs0UIpflo by aral@mastodon.ar.al
2026-01-29T13:31:42Z
0 likes, 0 repeats
So here’s a little JavaScript initialisation order quiz.Take the following code:```jsclass A { constructor (parameters = {}) { Object.assign(this, parameters) }}class B extends A { foo constructor (parameters) { super(parameters) this.foo ??= ‘no’ }}const b1 = new B()const b2 = new B({ foo: ‘ok’ })console.info(`${b1.foo}, ${b2.foo}`)```What output would you see if:1. You ran it as-is2. You changed the `foo` instance property declaration to `foo = this.foo ?? ‘yes’`3. You changed `foo` back and removed class B’s constructorTry and answer without running it first :)#JavaScript #quiz
(DIR) Post #B2mMjBbrsa4FHb1d68 by Professor_Stevens@mastodon.gamedev.place
2026-01-29T13:57:18Z
0 likes, 0 repeats
@aral I guessed wrong but, when I cheated, I couldn't get #2 to run.
(DIR) Post #B2mPmCkwNDY7ZMOqkC by nil@mastodon.sg
2026-01-29T14:31:35Z
0 likes, 0 repeats
@aral do you literally mean this? > You changed the `foo` instance property declaration to `foo = this.foo ?? ‘yes’`Or do you mean this.foo = this.foo ?? yes;Assuming the latter because otherwise the global is unused
(DIR) Post #B2mWE9hRvhhTvKThUO by aral@mastodon.ar.al
2026-01-29T15:43:49Z
0 likes, 0 repeats
@mdhughes Well, if we’re talking about “true”… there’s always bytecode ;)Turtles all the way down… :)
(DIR) Post #B2mWJTWXeT3Ur3ueIq by aral@mastodon.ar.al
2026-01-29T15:44:50Z
0 likes, 0 repeats
@Professor_Stevens Odd, so what error are you getting on:class A { constructor (parameters = {}) { Object.assign(this, parameters) }}class B extends A { foo = this.foo ?? 'yes' constructor (parameters) { super(parameters) this.foo ??= 'no' }}const b1 = new B()const b2 = new B({ foo: 'ok' })console.info(`${b1.foo}, ${b2.foo}`)
(DIR) Post #B2mWPkBkbcYsHEB2P2 by aral@mastodon.ar.al
2026-01-29T15:45:54Z
0 likes, 0 repeats
@nil The declaration, not the assignment. Although we are changing the declaration to a combined declaration and assignment so I can see how that could be confusing. So:class A { constructor (parameters = {}) { Object.assign(this, parameters) }}class B extends A { foo = this.foo ?? 'yes' constructor (parameters) { super(parameters) this.foo ??= 'no' }}const b1 = new B()const b2 = new B({ foo: 'ok' })console.info(`${b1.foo}, ${b2.foo}`)
(DIR) Post #B2mX5PgDFMhYphIKzg by Professor_Stevens@mastodon.gamedev.place
2026-01-29T15:53:24Z
0 likes, 0 repeats
@aral Ah, yes. That does run. I made the change in the wrong place. <doh!>