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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:37:56+00:00 2026-06-10T10:37:56+00:00

I am wondering can we still change the function body once it is constructed

  • 0

I am wondering can we still change the function body once it is constructed ?

     var O = function(someValue){
           this.hello = function(){
                return "hello, " + someValue;
           }
     }

     O.prototype.hello = function(){
           return "hhhhhhh";
     }

     var i = new O("chris");
     i.hello();   // -> this still returns the old definition "hello, chris"

The javascript statement O.prototype.hello = function(){....} doesn’t override and redefine the hello function behavior. Why is that ? I know it will have a type error if you tried to reuse the parameter someValue.

      // this will fail since it can't find the parameter 'someValue'
      O.prototype.hello = function(){
             return "aloha, " + someValue;
      } 

I am wondering why It allows to add function during runtime like

      O.prototype.newFunction = function(){
           return "this is a new function";
      }

      i.newFunction();   //  print 'this is a new function' with no problem.

but doesn’t allow you to change the definition once it’s defined.
Did i do something wrong ? how do we override and redefine a function within a class ? and is there a way to reuse the parameter that we passed in earlier to create the object ? in this cases how do we re-use someValue if we want to extend more functions to it.

  • 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-10T10:37:57+00:00Added an answer on June 10, 2026 at 10:37 am

    When you use new, the value of this inside the constructor points to the newly created object (for more information on how new works, take a look at this answer and this answer). So your new instance i, has a hello function. When you try to access the property of an object, it walks up the prototype chain until it finds it. Since hello exists on the instance of the object, there is no need to walk up the prototype chain to access the version of hello that returns hhhhhhhh. In a sense, you have overridden the default implementation in your instance.

    You can see this behavior if you don’t assign hello to this inside your constructor:

    var O = function(someValue) {
    
     }
    
     O.prototype.hello = function(){
           return "hhhhhhh";
     }
    
     var i = new O("chris");
     console.log(i.hello()); //this prints out hhhhhhh
    

    What you’re doing is kind of backwards. The prototype basically provides the “default” form of something, which you can override on a per-instance basis. The default form is used only if the property you’re looking for cannot be found on the object. That is, JavaScript will start walking up the prototype chain to see if it can find a property that matches what you’re looking for. It it finds it, it will use that. Otherwise, it will return undefined.

    What you basically have in the first case is as follows:

    Object.prototype.hello (not defined; returns "undefined")
    |
    +----O.prototype.hello (returns "hhhhhhhh")
         |
         +----i.hello (returns "hello, chris")
    

    So when you do i.hello, JavaScript sees that there is a hello property on i and uses that. Now if you didn’t explicitly define a hello property, you basically have the following:

    Object.prototype.hello (not defined; returns "undefined")
    |
    +----O.prototype.hello (returns "hhhhhhhh")
         |
         +----i.hello (is "undefined", so JavaScript will walk up the chain until 
                       it sees O.prototype.hello, which does have a defined value 
                       it can use.)
    

    What this means is that you can provide a default implementation in the prototype, and then override it (in a sense it’s like sub-classing). What you can also do is modify the behavior on a per-instance basis by directly modifying the instance. The version of hello that you have on the prototype is kind of a fail-safe and a fall-back.

    EDIT: Answers to your questions:

    Overriding on a per-instance basis means you attach a property or a function to a particular instance. For example, you could do:

    i.goodbye = function() {
        return "Goodbye, cruel world!";
    };
    

    Which means that this behavior is specific to that particular instance (i.e., only to i and not to any other instances that you may have created).

    If you take out this, then you have basically have:

    hello = function() {
        return "hello, " + someValue;
    }
    

    Which is equivalent to doing:

    window.hello = function() {
        return "hello, " + someValue;
    }
    

    So in this case, hello is a global reference to that function. What this means is that hello isn’t attached to any of your objects.

    hello can be undefined if you don’t have this.hello = function() { .... }; inside your constructor. I was also talking about the general process that JavaScript uses to try to resolve properties on objects. As I mentioned before, it involves walking up the prototype chain.

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

Sidebar

Related Questions

I am still new at SAS and I was wondering how I can do
I was wondering how can I test existing stored procedures if they are still
Just wondering if this plugin is still supported, cant find any contact details to
Ok I am still working on this jquery photo manager which you can see
I am just wondering how can I change RichTextArea inner font-family or font-size for
was wondering how can I create a sub drop down box upon selecting a
I was wondering how can I track the current logged in user in a
I was wondering how can I control the image on the connect dialog? I
i'm wondering how can I compare in C language, a number I put on
I was wondering how can I do something similar to Facebook when a link

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.