Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7955009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:36:14+00:00 2026-06-04T03:36:14+00:00

I’ve recently been learning javascript by writing some gnome shell extensions, and hence my

  • 0

I’ve recently been learning javascript by writing some gnome shell extensions, and hence my understanding of Javascript has been shaped by the examples I’ve observed in the gnome-shell javascript sources. I have a feeling I’ve been understanding classes wrong and just want some clarification.

I’ve written a few of my own subclasses, and in each case I’ve defined them simply by following similar code in the gnome-shell javascript source:

Subclass = function() {
    this._init.apply(this,arguments);
}
Subclass.prototype = {
    __proto__: Superclass.prototype,
    _init: function() {
        Superclass.prototype._init.call(this);
    },
    // add other methods of Subclass here.
}

Up to now I thought this was the standard way of making a class Subclass that was basically Superclass plus extras. I assumed that every object had a _init method.

I’ve recently tried to apply the same method to make a subclass of a Clutter.Actor (what’s important is that it’s not a GNOME-shell-defined class), and realised that the above way of subclassing objects is not the standard. For one, not every class has a _init function as I assumed; this is just something that GNOME-shell has done in their javascript classes.

So, my questions are:

  1. Is there any documentation regarding the above method of creating subclasses? All the tutorials I’ve seen say to set Subclass.prototype = new Superclass() instead of doing the Subclass.prototype = { __proto__:Superclass.prototype, define_prototype_methods_here } method, but my thought is that there must be some method to it if gnome-shell uses it consistently?
  2. If I wanted to stay as close as possible to the above way of defining classes (just so I can retain some code similarity to GNOME-shell for which I am writing extensions), what do I replace Superclass.prototype._init.call(this) with in Subclass._init to make sure that the Subclass.prototype gets all the methods/properties of Superclass (which I then add on to in my definition of Subclass.prototype), if the Superclass doesn’t have an _init function (i.e. does it have some equivalent constructor function I call)?

I’m really confused about this all so please forgive me if my question doesn’t make much sense; it’ll be because of the extent of my misunderstanding & confusion!

EDIT: clarification:
– I know the __proto__ is not recommended because it is non-standard, but my code is never going to run in a browser – it’s only ever going to run with GNOME javascript (which is basically the Mozilla javascript engine), so I don’t need to worry about cross-compatibility.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T03:36:15+00:00Added an answer on June 4, 2026 at 3:36 am

    As already said, don’t use __proto__. It’s a non-standard property. (It’s standardized for JavaScript in browsers now. Still don’t use it.) But

    Subclass.prototype = new Superclass(); // Don't do this
    

    is not a very good method either. What if Superclass expects parameters?

    You have better options.

    ES2015 and above

    class handles all of this plumbing for you; complete example:

    class Superclass {
        constructor(superProperty) {
            this.superProperty = superProperty;
        }
        method() {
            console.log("Superclass's method says: " + this.superProperty);
        }
    }
    class Subclass extends Superclass {
        constructor(superProperty, subProperty) {
            super(superProperty);
            this.subProperty = subProperty;
        }
        method() {
            super.method(); // Optional, do it if you want super's logic done
            console.log("Subclass's method says: " + this.subProperty);
        }
    }
    
    let o = new Subclass("foo", "bar");
    console.log("superProperty", o.superProperty);
    console.log("subProperty", o.subProperty);
    console.log("method():");
    o.method();

    ES5 and earlier

    Let Subclass.prototype inherit from Superclass.prototype only. This can be done for example with ES5’s Object.create:

    Subclass.prototype = Object.create(Superclass.prototype);
    Subclass.prototype.constructor = Subclass;
    

    And then in Subclass, you call Superclass with this referring to the object so it has a chance to initialize:

    function Subclass() {
        Superclass.call(this); // Pass along any args needed
    }
    

    Full example:

    function Superclass(superProperty) {
        this.superProperty = superProperty;
    }
    Superclass.prototype.method = function() {
        console.log("Superclass's method says: " + this.superProperty);
    };
    function Subclass(superProperty, subProperty) {
        Superclass.call(this, superProperty);
        this.subProperty = subProperty;
    }
    Subclass.prototype = Object.create(Superclass.prototype);
    Subclass.prototype.constructor = Subclass;
    
    Subclass.prototype.method = function() {
        Superclass.prototype.method.call(this); // Optional, do it if you want super's logic done
        console.log("Subclass's method says: " + this.subProperty);
    };
    
    var o = new Subclass("foo", "bar");
    console.log("superProperty", o.superProperty);
    console.log("subProperty", o.subProperty);
    console.log("method():");
    o.method();

    ES3 and earlier

    ES3 and earlier don’t have Object.create, but you can easily write a function that sets up the prototype for you in much the same way:

    function objectCreate(proto) {
        var ctor = function() { };
        ctor.prototype = proto;
        return new ctor;
    }
    

    (Note: You could half-shim Object.create by creating one that takes only one argument, but the multiple-argument version of Object.create cannot be shimmed, so it would be giving other code on the page the wrong idea if it also uses Object.create.)

    Then you do the same thing as our ES5 example:

    Full example:

    function objectCreate(proto) {
        var ctor = function() { };
        ctor.prototype = proto;
        return new ctor;
    }
    
    function Superclass(superProperty) {
        this.superProperty = superProperty;
    }
    Superclass.prototype.method = function() {
        console.log("Superclass's method says: " + this.superProperty);
    };
    function Subclass(superProperty, subProperty) {
        Superclass.call(this, superProperty);
        this.subProperty = subProperty;
    }
    Subclass.prototype = objectCreate(Superclass.prototype);
    Subclass.prototype.constructor = Subclass;
    
    Subclass.prototype.method = function() {
        Superclass.prototype.method.call(this); // Optional, do it if you want super's logic done
        console.log("Subclass's method says: " + this.subProperty);
    };
    
    var o = new Subclass("foo", "bar");
    console.log("superProperty", o.superProperty);
    console.log("subProperty", o.subProperty);
    console.log("method():");
    o.method();
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.