Access to static properties via this.constructor in typescript

but in typescript this.constructor.prop causes error “TS2339: Property ‘prop’ does not exist on type ‘Function’”. Typescript does not infer the type of constructor to be anything beyond Function (after all … the constructor might be a sub class). So use an assertion: class SomeClass { static prop = 123; method() { (this.constructor as typeof SomeClass).prop; … Read more

How does require() in node.js work?

Source code is here. exports/require are not keywords, but global variables. Your main script is wrapped before start in a function which has all the globals like require, process etc in its context. Note that while module.js itself is using require(), that’s a different require function, and it is defined in the file called “node.js” … Read more

Why does String.prototype log it’s object like a standard object, while Array.prototype logs it’s object like a standard array?

Because in a method call the this argument is always (in sloppy mode) casted to an object. What you see is a String object, which was produced from the “test” primitive string value. The array on which you call your method is already an object, so nothing happens and you just get the array as … Read more

what is ‘this’ constructor, what is it for

It’s used to refer to another constructor in the same class. You use it to “inherit” another constructor: public MyClass() {} public MyClass(string something) : this() {} In the above, when the second constructor is invoked, it executes the parameterless constructor first, before executing itself. Note that using : this() is the equivalent of : … Read more

Passing “this” in java constructor

Passing this to another method/object from inside the constructor can be rather dangerous. Many guarantees that objects usually fulfill are not necessarily true, when they are looked at from inside the constructor. For example if your class has a final (non-static) field, then you can usually depend on it being set to a value and … Read more