I'm trying to deal with OOP in js

I encountered such a problem. There are 2 classes, one is inherited from another through the prototype class of the parent. The set method is added. When I try to call this method, the successor js says there is no such method.

Example:

function GameObject(){} GameObject.prototype.set = function() {}; function GameFon() {} GameFon.prototype = GameObject; GameFon.prototype.constructor = GameFon; var fon = new GameFon(); fon.set(); //здесь возникает ошибка 

What could be the problem?

    2 answers 2

    Now it's easier to use es6 translator than not to use es6

     'use strict' class GameObject { set(value) { this._value = value; } get() { return this._value; } } class GameFon extends GameObject { test() { this.set("Hello Class"); console.log(this.get()); } } new GameFon().test() 

    (Works in non-ancient chrome.)

    • In order to self-development, and experimental approaches to design, I can advise you to read the article about 3D cubes, originalip.ru/?L=58 link to the source (EN) you can find at the end to become. Good luck! - Sergey Kashurin
    • I did not understand something about how he relates to the topic, in general, in my opinion, they were too smart with the implementation. - zb '
    • Awful way. - Qwertiy
    • Well, yes, 3 years ago, however. I do not remember, maybe there is something suitable for a particular case - zb '26
    • @Qwertiy Or maybe I didn’t write at all, the history of edits isn’t, but it looks like crap even three years ago - zb '26

    It is necessary to put in the prototype an intermediate object based on the prototype of the base class:

     GameFon.prototype = GameObject; 
     GameFon.prototype = Object.create(GameObject.prototype); 
    • In general, now just a class extend - zb '