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;
    }

})();
7
  • 2
    This is how people used to write javascript in 2000s, before modules and classes. No need to learn and use this pattern today. Commented May 11, 2021 at 9:08
  • Just use the class syntax and transpile your code. Avoids way too many useless details around the old style OO-ish code. Commented May 11, 2021 at 9:08
  • 1
  • 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. Commented May 11, 2021 at 9:49
  • 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. 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. Commented May 11, 2021 at 10:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.