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 6639673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:34:53+00:00 2026-05-25T23:34:53+00:00

Ignoring the fact that this is implemented by many frameworks.. What is the best

  • 0

Ignoring the fact that this is implemented by many frameworks..

What is the best way to prototype for multiple object types.

For example, Should I write:

Element.prototype.bind = function(eventType, fn) {
    this.addEventListener(eventType, fn, false);
};
NodeList.prototype.bind = function(eventType, fn) {
    for (var i = 0, l = this.length; i < l; i++) {
        this[i].addEventListener(eventType, fn, false);
    }
};

or

Object.prototype.bind = function(eventType, fn) {
    if (this instanceof Element) {
        this.addEventListener(eventType, fn, false);
    } else if (this instanceof NodeList) {
        for (var i = 0, l = this.length; i < l; i++) {
            this[i].addEventListener(eventType, fn, false);
        }
    }
}
  • 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-05-25T23:34:54+00:00Added an answer on May 25, 2026 at 11:34 pm

    Big red flag: never extend Object.prototype! It breaks for (x in y) (or at least people’s expectations of how it works).

    In the general case, however, it’s better to extend the individual types’ prototypes rather than try to find a common base and perform reflection checks. There are two basic reasons to do this.

    First, JavaScript is a dynamically typed language, so you should generally prefer duck typing and basic inheritance over instanceof and reflection. In many cases, there is a small performance benefit, but mainly this is so you don’t spend time fighting the language.

    Second, this type of method switching implies connections between the switched methods which do not necessarily exist. In your sample code, this can be seen in the NodeList version. Conceptually, a list shouldn’t be binding anything; if it has a bind method, it should simply translate into calling the bind method on each of its elements. This ensures that if any extra steps need added to binding in the future (say, workarounds for old, non-DOM-conforming browsers…), you only need to do so in one place.

    But looking at it in terms of type switching in the “same function” gives the false impression that both Element.bind and NodeList.bind should work the same way — by calling addEventListener. Here’s how I’d implement it:

    Element.prototype.bind = function(eventType, fn) {
        this.addEventListener(eventType, fn, false);
    };
    
    NodeList.prototype.bind = function(eventType, fn) {
        for (var i = 0, l = this.length; i < l; i++) {
            this[i].bind(eventType, fn);
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Scenario Ignoring that fact that some of the settings might be wrong and inconsistent
Ignoring unsafe code, .NET cannot have memory leaks. I've read this endlessly from many
In C++, you can pass into a function an object that is const. This
Ignoring padding/alignment issues and given the following struct, what is best way to get
This is probably a bit subjective, but I'm in fact looking for answers that
Why does this empty the text immediately (ignoring delay)? $('#error_box_text').html('error text').delay(5000).html('') # jQuery 1.4
Based on this , how would you make ignoring parameters more succint? var m
I have a problem finding elements in XPath that's contains a certain string ignoring
One of my pet peeves with raising events in C# is the fact that
For example, I'm writing tests against a CsvReader. It's a simple class that enumerates

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.