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

  • Home
  • SEARCH
  • 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 8033893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:50:41+00:00 2026-06-05T01:50:41+00:00

The prototype of some native javascript objects can be extended to include new functions

  • 0

The prototype of some native javascript objects can be extended to include new functions (such as NodeList.prototype.forEach or NodeList.prototype.addEventListener) which I use to allow array and element-like interaction with the NodeList. So far so good, but how should I add an object to the prototype which in turn has it’s own functions (to allow NodeListVar.classList.remove("class")). I have been able to make NodeListVar.classList().remove("class"), by doing the following

NodeList.prototype.classList = function(){
    var _this = this;
    return {
        remove: function(class){
            _this.forEach(function(){
                this.classList.remove(class);
            });
        }
    }   
};

However I would greatly prefer the syntax to be the same as a normal element, thus more like:

NodeList.prototype.classList = {
    remove: function(class){
        //where *this* would be the nodeList and *not* the DOMWindow
        this.forEach(function(){
            this.classList.remove(class);
        });
    }
};

It probably isn’t hard even, but I have searched google endlessly and looked through countless questions already and can’t find anything usefull.

  • 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-05T01:50:43+00:00Added an answer on June 5, 2026 at 1:50 am

    First to read: What’s wrong with extending the DOM.

    You can’t set objects on a prototype. All called functions will be executed in context of that static prototype object, not the NodeList itself. The object on the prototype has no reference to the current NodeList.

    On normal Elements, every element has its own classList attribute, a DOMTokenList bound the element. You will need to do the same: Give every NodeList its own classLists instance. As you can’t do that in the unavailable constructor, you will have to use a getter, as already demonstrated.

    I don’t think you should try to allow the same syntax on NodeLists as on Elements, because they are very different. But if you want to get rid of these brackets, you can install a native getter function.

    function ClassLists(nl) {
        ... // nl references the NodeList we're bound to
    }
    ClassLists.prototype = ...;
    
    Object.defineProperty(NodeList.prototype, "classLists", {
        get: function() {
            return this.classLists = new ClassLists(this);
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think I'm missing some key concept regarding objects and prototype functions in JavaScript.
Hi I am converting some prototype javascript to jQuery javascript. In prototype we can
I have some (library API, so I can't change the function prototype) function which
I am try to write some validation script using javascript and prototype. What I
I'm using JavaScript Prototype (through Ruby on Rails) to handle some Ajax calls; but
I am writing a prototype application. Right now some things fail such as inserting
I am mlearning javascript and have some trouble creating an onject via prototype. I
I realize that adding methods to native JavaScript objects (Object, Function, Array, String, etc)
What are some non prototype modifying libraries that provide core functionality to JavaScript (not
A few people I work with include on their sites some javascript I wrote

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.