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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:03:07+00:00 2026-05-27T11:03:07+00:00

Basically, I want to know how/what’s the difference between overriding/adding methods on instances of

  • 0

Basically, I want to know how/what’s the difference between overriding/adding methods on instances of objects vs methods on the ‘Class’ instance of JS objects (yes class isn’t the right word, not sure what is though).

For example, why doesn’t this work?

Date.prototype.now= function (){
    return 23;
}

$('#foo').append(Date.now());    

And yet, this does?

Array.prototype.indexOf = function(){
    return 23;
}
var bar = Array();
bar[0] = "a";
$('#bar').append(bar.indexOf("a"));

http://jsfiddle.net/tad604/fMWKA/2/

  • 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-27T11:03:08+00:00Added an answer on May 27, 2026 at 11:03 am

    now is a static method on Date. It is not an instance method; not defined on Date.prototype to begin with. It is defined directly on the Date object. When you write

    Date.prototype.now = function () {
        return 23;
    }
    

    you are not actually overwriting the function called by Date.now().


    On the other hand, indexOf for arrays is an instance method. That is, it’s defined on Array.prototype, so when you write

    Array.prototype.indexOf = function(){
        return 23;
    };
    

    you are in fact changing the indexOf function called by

    var bar = [];
    bar.indexOf('a');
    

    See the difference? http://jsfiddle.net/mattball/7qVQy/


    I recommend reading about the JavaScript prototype lookup chain, which should clarify some of your confusion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I want to know why this works: class MyClass: pass myObj = MyClass()
Basically I want to know how to set center alignment for a cell using
Basically, I want to know what is best to avoid future problems and confusions
Basically, I just want to know if its possible to use Nhibernate to migrate
So I basically want to have a static method in my AR class to
I am new to networking. I just basically want to know whether we can
Basically I want to know how many people have tweeted a link to a
I have an iframe setup within a page and basically want to know whether
Basically I want to know if a given nodes children are visible in a
Basically I want to know why as.Date(200322,format=%Y%W) gives me NA . While we are

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.