852 questions
0 votes
0 answers
29 views
Modifying the prototype chain affects the constructor property in JavaScript [duplicate]
In JavaScript, Object inherits properties through the prototype chain. Example : // Parent Function function Person(name, age) { this.name = name; }; Person.prototype.hello = function() { console....
1 vote
1 answer
115 views
Giving a type to polymorphic static methods used as public constructors
I have a number of classes all implementing the same interface. In order to create instances of these classes one should not use the constructor but rather a number of static methods which take a ...
0 votes
1 answer
67 views
Prototype assignment within a factory in order to achieve private field handling versus constructor and private state without prototypal methods
I know there have been a lot of questions about module patterns, prototypical inheritance, ES6 classes, etc with excellent answers. This is not about discussing different patterns or the bad sides of ...
2 votes
3 answers
588 views
Why does TS allow interface multiple inheritance?
I'm learning TypeScript about extending the interface. I unintentionally recognized that TypeScript allows extending interface from multiple classes. That made me surprised and I have researched a lot ...
0 votes
1 answer
74 views
Add event handlers like "onclick" or "onchange" as public class field on a customized built-in element
I have standard handlers for the click, input and change events of my customized HtmlInputElement. I want to define these handlers like this: class MyInputElement extends HTMLInputElement { ...
0 votes
1 answer
45 views
What is the correct way to inherit from a prototype?
I have 2 "classes": const Person = function(firstName, birthYear){ this.firstName = firstName; this.birthYear = birthYear; } Person.prototype.sayHi = function(){ console.log('Hi!'); } ...
1 vote
2 answers
139 views
Is there a static version of `instanceof`?
Is there a static equivalent of instanceof? I.E., rather than: obj1 instanceof Type something like: TypeA instanceof TypeB? I couldn't find anything on the matter, so I hacked together this: function ...
0 votes
1 answer
48 views
Js Prototype inheritance, is Object.create necessary? [duplicate]
function Animal(name) { this.name = name; } Animal.prototype.printName = function() { console.log(`my name is ${this.name}`) } function Dog(name) { Animal.call(this, name); this.speak =...
0 votes
2 answers
115 views
Why I can't use 'super' in prototype method definition when I can use Object.getPrototypeOf(this.constructor.prototype)?
So I tested this code: class C1 {} C1.prototype. f =function(){ return 1 } class C2 extends C1 {} C2.prototype. f =function(){ return super.f()+1 } And it throws a syntax error: 'super' keyword ...
0 votes
1 answer
75 views
On the prototype chain of an object, some objects' .constructor.prototype doesn't point to its [[prototype]], but points to that object itself
The source object that printing the prototype chain can be various: [1, 2, 3] // array literal {a: 1} // object literal new Set() // built-in set new Promise( () =>{} ) // built-in Promise ...
1 vote
1 answer
208 views
Can't overload the = assignment operator in derived class (as I want); call from main jumps to base class operator method
I'm trying to implement a class for a matrix with all the usual operations, just as an exercise. I implemented the common operations (+, -, ·) but my problem came when I tried to implement slicing ...
1 vote
0 answers
122 views
Is there a magic keyword that can allow me to ‘implicitly’ access all base class members? [duplicate]
I'm using a derived class from a base class using templates. I have something like: #include <iostream> using namespace std; template <typename T> class BaseClass { public: ...
0 votes
0 answers
22 views
Prototypal Inheritance Representation in Developer Console When Using `Object.create` [duplicate]
I have a simple piece of JavaScript code creating object b using Object.create and passing object a as the argument: const a = {}; const b = Object.create(a); console.log(b); It is my understanding ...
2 votes
3 answers
85 views
How can I refactor following code with prototypical inheritance?
I have a following code: const myFunc = () => { let var1, var2, var3; const setVar1 = (val) => { var1 = val } return { setVar1 } } Can you please tell me how this pattern is ...
0 votes
0 answers
56 views
Using closures to manually "extend" a class in JavaScript/TypeScript
This isn't so much a problem I'm having, rather than a general question about how classes/prototypes work in JS vs using closures. Lets say I have the following class (I'm going to omit the full body'...