0

I often see the following syntax in libraries, but then no inheritance. Now how can I define an ObjB that is inherited from ObjA and adds its own variables and methods?

(function() {

    'use strict';

    var ObjA = function(o) {
        var self = this;

        self.init(o);
    };

    ObjA.prototype = {
        init: function(o) {
            var self = this;

            self._x = o.x || false;
            self._y = o.y || false;

            return self;
        }
        // More methods...
    }

    if (typeof global !== 'undefined') {
        global.ObjA = ObjA;
    } 
    else if (typeof window !== 'undefined') {  
        window.ObjA = ObjA;
    }

})();
root66
  • 477
  • 5
  • 17
  • 2
    This is how people used to write javascript in 2000s, before modules and classes. No need to learn and use this pattern today. – georg May 11 '21 at 09:08
  • Just use the `class` syntax and transpile your code. Avoids way too many useless details around the old style OO-ish code. – VLAZ May 11 '21 at 09:08
  • @georg know there are more modern options. It is only for educational purposes. – root66 May 11 '21 at 09:34
  • 1
    For educational purposes, start with some light reading: [JavaScript Inheritance](https://stackoverflow.com/q/7486825) | [How do I inherit javascript functions ?](https://stackoverflow.com/q/7539148) | [What is “inheritance” in Javascript?](https://stackoverflow.com/q/5027045) | [Performing inheritance in JavaScript](https://stackoverflow.com/q/1586915) | [JavaScript efficient solution for multi-inheritance](https://stackoverflow.com/q/47399264) | [JavaScript Inheritance with Prototypes — 'constructor' property?](https://stackoverflow.com/q/22315909) – VLAZ May 11 '21 at 09:46
  • 1
    Not all of these are directly related but therein lies the biggest problem - there is no one true inheritance. There are various ways to implement inheritance without using the class syntax. Some more wrong than others, some more suitable for certain situations over others. After you're done with the above links, go and read a lot more about more and more ways to implement inheritance. In a about a week you'd have dozens of approaches. And growing. You'd be well educated. With most of the knowledge potentially being subtly wrong because of subtle nuances between the approaches. – VLAZ May 11 '21 at 09:49
  • @VLAZ You are right, of course, but then what is the point of this platform? There are usually numerous solutions for problems and your type of answer could be sent as an autoresponder for all posts. – root66 May 11 '21 at 10:28
  • 1
    "*what is the point of this platform?*" to provide a repository of Q&As of practical problems. We already have quite a few to do with [inheritance in pre-ES6 JavaScript](https://stackoverflow.com/search?q=%5Bjavascript%5D+inheritance+-%5Becmascript-6%5D+is%3Aq). It seems the platform is doing its job just fine. What you don't seem to have is a practical problem that's not covered by existing questions. – VLAZ May 11 '21 at 10:37

0 Answers0