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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:25:10+00:00 2026-05-26T06:25:10+00:00

I have a fiddle to help me in my understanding of JavaScript prototyping and

  • 0

I have a fiddle to help me in my understanding of JavaScript prototyping and inheritance. The comments tell the story.

//From Douglas Crockford's "Javascript: the good parts":  a helper to hide the "ugliness" of setting up a prototype
Function.prototype.method = function(name,func) {
    this.prototype[name] = func;
    return this;
}

function SomeFunc(value) {
    this.setValue(value);
}

//Inherit the function (to me this conceptually the same as adding a member to a class)
SomeFunc.method('setValue', function (value) {
    this.value = value;
    return this;
});

try
{
SomeFunc(1);
}
catch(e)
{
    alert(e);
}

Why do I get an exception? Are my notes correct in that what JavaScript calls inheritance is to a Classical programmer simply adding a new member to a class? What is the difference between augmentation and inheritance?

  • 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-26T06:25:10+00:00Added an answer on May 26, 2026 at 6:25 am

    Are my notes correct in that what JavaScript calls inheritance is to a Classical programmer simply adding a new member to a class?

    No, not really. In JavaScript, we have prototype-based inheritance. That means an object has a reference to another certain object, its prototype. Whenever a property of an object is accessed, the prototype chain is searched for this property. So if the the object does not have this property itself, its prototype is inspected for this property and so on.

    +------------+    +-------------+
    |  Instance  |    |  Prototype  |
    | __proto__ -+--->|  __proto__ -+-->...
    | foo        |    |  bar        |
    +------------+    +-------------+
    

    Instance has both, the foo and bar properties.

    Now, if you have a constructor function, you can create many instances (objects) referring to the same prototype. When you now add a new property to that prototype, all instances will have this property too (due to the prototype chain).

    This is often done to dynamically extend instances but it is only one consequence of prototype inheritance, it is not inheritance itself.

    What is the difference between augmentation and inheritance?

    Inheritance would be to set the prototype of an object to a certain object so that it is in the prototype chain. Augmentation is just copying properties. The object would own that property then:

    +------------+
    |  Instance  | 
    | __proto__ -+--->...
    | foo        |    
    | bar        |
    +------------+
    

    Why do I get an exception?

    Because you are calling SomeFunc like a “normal” function, not like a constructor function. In that case, this will refer to window, which does not have a setValue method.

    Instead you want to call it with the new operator [MDN], to create a new instance:

    var instance = new SomeFunc(1);
    

    If called this way, this will refer to an empty object which inherits from the constructor function’s prototype property (SomeFunc.prototype).

    MDN has a nice article about this and what it refers to in certain situations.

    And if you look at what method is doing, you see that it adds a new property to SomeFunc.prototype (this refers to SomeFunc):

    this.prototype[name] = func;
    

    In fact, was the first part of your code is doing is what I mentioned above: By extending Function.prototype, you add a new property to each function and this lets you call SomeFunc.methods later.


    Further reading:

    • MDN – Inheritance and the prototype chain
    • MDN – Working with Objects
    • MDN – this
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the fiddle all ready to go I just need help with the
I have following fiddle: http://jsfiddle.net/BFSH4/ As you see there are two issues: The h1
I have this fiddle: http://jsfiddle.net/yub2B/4/ HTML: <input type=text /> <input type=text /> <input type=text
I have this fiddle : http://jsfiddle.net/XjeCf/1/ that works like I want and this one
I have a fiddle : http://jsfiddle.net/SDr3F/10/ Currently: onclick, it alerts alert('hi') Required: onclick, it
I have a fiddle here - http://jsfiddle.net/hhimanshu/SDr3F/2/ The left pane is already available I
I have a fiddle here http://jsfiddle.net/WULsZ/1/ I load jQuery first and the code is
i have a very conceptual question regarding jquery. i have this fiddle http://jsfiddle.net/zvCrN/ What
Ok, I have this fiddle http://jsfiddle.net/25J3M/6/ , I want to position the red and
see fiddle i have html table and one textbox and one button.make cell selection

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.