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

The Archive Base Latest Questions

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

I just started reading on Douglas Crockford’s Javascript The Good parts where he explains

  • 0

I just started reading on Douglas Crockford’s “Javascript The Good parts” where he explains about Augmenting basic types.

Function.prototype.addMethod=function(name,func) {
    this.prototype[name]=func; 
    return this; 
};

The moment after doing this, the addMethod becomes available for all basic objects like String, Number etc. That leaves me puzzled

  1. Why does this happen when I haven’t added it to Object.prototype?

  2. Why adding a method to Function.prototype gets reflected in all basic objects?

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

    He probably meant The moment after doing this, the addMethod becomes available for all basic objects object types like String, Number etc. This is because the String object is a function (but objects created by String are not).

    E.g., given

    var s = '';
    

    You can do

    String.addMethod(...);
    

    but not

    s.addMethod(...);
    

    A brief explanation of the JavaScript type system comes here:

    JavaScript does not have the normal concept of classes. Instead, you can achieve somewhat the same by turning any function into a constructor by puttin the new keyword in front of it when it is invoked.

    E.g: given

    function MyFunction(x) { this.myX = x; }
    

    if you invoke it like

    var myObj = new MyFunction(10);
    

    it will create an object called myObj. This object will have a single member variable called myX. The function MyFunction is considered the constructor of the object (and is stored in the “constructor” property.

    (Bonus question: What will happen if you invoke the function above without the new keyword, i.e. var x = MyFunction(10). The answer will probably surprise any sensible person.)

    Now you have seen how any arbitrary function can be turned into a constructor. The built-in objects are exactly the same, string objects are created by the function String, numbers are created by the function Number, etc.

    Just as those built-in objects are created by functions, each of those functions are also created by the “Function” function (yikes!).

    Now on to prototypes.

    in the example above, if you somewhere write

    MyFunction.prototype.someNewMethod = function() {}
    

    all objects created by the MyFunction constructor/function will appear to have an extra member function called someNewMethod. You can do many other funky things with prototypes, like replacing the prototype, or replacing the prototype’s prototype, but I’m not an expert at that.

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

Sidebar

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.