I’m currently working on a browser extension to manage opened tabs and I notice that in JS ES polymorphism works a bit strange when I declare class fields at the top of the class. Let say that we want to use polymorphism in object initialization. E.g. we have base class View: and derived class TabView: Now lets try to call
Tag: oop
Concatenative inheritance vs class inheritance in JavaScript
concatenative inheritance works like a composition to me when I look to it at the beginning, but people keep naming it as an inheritance. classes, however, use the prototype to create a prototype chain that connects objects together. the question now is, if both concatenative inheritance and class inheritance do the same thing which one to use? here is an
Making class instance reactive in Svelte using stores
I am learning Svelte by creating simple app. The logic is written using classes. The idea is, that all the data needed comes from class instance properties. Instances should not be instantiated more than once. I am using stores to provide components this instances. The problem is I can’t get reactivity using this approach. I tried readable and writable stores
What is the difference between class fields and properties in Javascript
I’m reading class fields proposal for JavaScript. I don’t understand why the authors call it ‘fields’ and not properties. MDN docs in class article speak about instance properties declared inside constructor and in next section about field declarations declared using new syntax. What is the difference between the two besides the syntax? Answer What is the difference between the two
Access an instance from a function outside of the object constructor
I have a problem I can’t understand after a lot of attempts to solve it. To help you understand, there are 2 classes (Game and Board), and a third file with the jQuery keypress controls. Game is about the logic of the game, and Board about the display. Here is a part of the code I hope sufficient to understand.
Cannot assign to read only property ‘name’ of object ‘[object Object]’
The following code will throw an error only for the name property. It could be fixed by specifying name property as writable in Object.create arguments but I’m trying to understand why is this happening(and maybe there is a more elegant way to fix it). Answer You cannot modify the name property of a function. The descriptor says it is not
What is the end of prototype chain in JavaScript — null or Object.prototype?
I’ve been reading about the prototype chain in JavaScript and came to two slightly different definitions. It is said that every object in JavaScript has a prototype and that prototype in turn has another prototype. The top prototype (Grand) may also have prototype and the chain can continue. Now the chain will stop at one last object. JavaScript: The Good
Find all classes in a Javascript application that extend a base class
I have code like this I want to look at all of the classes in my application’s universe, and when I find one that descends from Animal, I want to create a new object of that type and add it to the list. This allows me to add functionality without having to update a list of things. So I can
Using `this` for Parent Function inside a Nested Function
In the Apple “classs”, how can the nested function countSeeds() retrieve the value this.price? jsfiddle: http://jsfiddle.net/VGqEa/ Output Answer put var self = this at the top of Apple, and then refer to this as self instead in the nested function. i.e.: You could also put the self=this statement at the beginning of this.cutOpen, since this will still refer to the
What is the best way to define dependent variables in an object?
In the Google developers recommendation for optimizing JavaScript code, they mention that the best way to declare/initialize new variables for object is to use the prototype. For instance, instead of: Use: However, in my case I have a dependency between variables, for instance: Using this.toWhom outside of the main constructor body or a method function of the object will yield