Private Fields Use # Symbol?!

Private fields are HIDDEN! The # symbol makes properties truly private. You can't access them from outside, even with bracket notation. This is REAL privacy!

#javascript #javascripttricks #privatefields #symbol #encapsulation #javascriptweird #javascriptquiz #codingchallenge #javascriptshorts #javascriptwtf #classprivacy #advancedjavascript

https://www.youtube.com/watch?v=ycxs4Sb_7Eo

Private Fields Use # Symbol?! #codingchallenge

YouTube

What is the difference between extending a subclass of an object and `Object.create`-ing based on a prototype for the purposes of JS private fields? I'm surprised to get different behavior between:

```javascript
class Foo {
#foo = 'foo';

doSomething(): void {
console.log(this.#foo);
}
}

class Bar extends Foo {}

new Foo().doSomething(); // Works
new Bar().doSomething(); // Works
Object.create(new Foo()).doSomething(); // ERROR: Cannot read private member.
```

In my mind, they should create basically the same prototype inheritance. Why does `Object.create` fail?

https://www.typescriptlang.org/play/?target=99#code/MYGwhgzhAEBiD29oG8BQ1oGIBmjoF5oByXeIgblXWgBN4BleAWwFMAXACwEsA7AcwAUASgBc0AG7wuNFNQzB4PCPBAsAdCHiDOXCGpyIhlDAF9UZ1KEgwAQmABO0FgA82LHjRgIkyCzxYA7nCIwmp0jKw6-MLk0AD0cdAA6vD2ANYQqP5Bdvah4czs3NFG8Ykp6ZkA8gBGAFYswGxqwPYsYG4C2cHwwkJhDIVRgqUJ0ACiAEqTVZNiAMJgPDzwbNBtYDIADvZc4h0s0KxMNSz2akA

#JavaScript #PrivateFields

TS Playground - An online editor for exploring TypeScript and JavaScript

The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.