Angular: 7.2.1 ES6 class ReferenceError : Cannot access ‘X’ before initialization
I was getting this error due to a circular dependency, like A injected with B B injected with C C injected with A Removing the circular dependecy fixed this error.
I was getting this error due to a circular dependency, like A injected with B B injected with C C injected with A Removing the circular dependecy fixed this error.
Can I do async constructor() No, that’s a syntax error – just like constructor* (). A constructor is a method that doesn’t return anything (no promise, no generator), it only initialises the instance. And, if not how should a constructor work that does this Such a constructor should not exist at all, see Is it … Read more
Yes, all methods of classes are non-enumerable by default. You still can iterate them using Object.getOwnPropertyNames. Filter out .prototype, .name and .length (or just everything that is not a function). To include inherited static methods, you will have to walk the prototype chain explicitly (using Object.getPrototypeOf).
You can use toJSON method to customise the way your class serialises to JSON: class MyClass { constructor(property) { this.property = property } set property(prop) { // Some validation etc. this._property = prop } get property() { return this._property } toJSON() { return { property: this.property } } }
It’s important to know that this syntax: class A { method = () => {} } is just syntactic sugar for creating an instance method in the class constructor: class A { constructor() { this.method = () => {} } } Note: This syntax is not an official part of the JavaScript language yet (currently … Read more
The reasoning is simple but not necessarily self evident. .then() returns a promise if then is called on a subclass of Promise, the returned promise is an instance of the subclass, not Promise itself. the then returned promise is constructed by calling the subclass constructor, and passing it an internal executor function that records the … Read more
If you don’t initialize state and you don’t bind methods, you don’t need to implement a constructor for your React component. The constructor for a React component is called before it is mounted. When implementing the constructor for a React.Component subclass, you should call super(props) before any other statement. Otherwise, this.props will be undefined in … Read more
It is complicated; I tried a lot! In the end, this one-liner worked for my custom ES6 class instances: let clone = Object.assign(Object.create(Object.getPrototypeOf(orig)), orig) It avoids setting the prototype because they say it slows down the code a lot. It supports symbols but isn’t perfect for getters/setters and isn’t working with non-enumerable properties (see Object.assign() … Read more
Binding is not something that is specifc to React, but rather how this works in Javascript. Every function / block has its own context, for functions its more specific to how its called. The React team made a decision for this to not be bound on custom methods on the class (aka not the builtin … Read more
Both ways are viable, but they do different things when it comes to inheritance with an overridden static method. Choose the one whose behavior you expect: class Super { static whoami() { return “Super”; } lognameA() { console.log(Super.whoami()); } lognameB() { console.log(this.constructor.whoami()); } } class Sub extends Super { static whoami() { return “Sub”; } … Read more