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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:07:06+00:00 2026-06-05T11:07:06+00:00

I’m trying to implement prototypal inheritance but I’m not understanding it’s behavior. Consider the

  • 0

I’m trying to implement prototypal inheritance but I’m not understanding it’s behavior.

Consider the following example:

var config = {
  writable: true,
  enumerable: true,
  configurable: true
};

var defineProperty = function( obj, name, value ) {
  config.value = value;
  Object.defineProperty( obj, name, config );
}

var man = Object.create( null );
defineProperty( man, 'sex', 'male' );

var yehuda = Object.create( man );
defineProperty( yehuda, 'firstName', 'Yehuda' );
defineProperty( yehuda, 'lastName', 'Katz' );

When I access to yehuda.sex returns male which is correct, but when I try to update the value what actually happens it’s the creation of a new property sex on yehuda.

One possible solution is to access directly the prototype property (Object.getPrototypeOf(yehuda).sex = 'female') but that implies that I need to know the property to which object belongs.

  • 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-05T11:07:07+00:00Added an answer on June 5, 2026 at 11:07 am

    The idea of using prototypes is that when you try to access a property on an object the interpreter will first try to find that property on the given object.

    If the property is not found on the given object then the interpreter will try to find it on the object’s prototype and so on until the prototype chain ends in null.

    If the property is not found the interpreter returns undefined. Otherwise it returns the property value.

    In your case when you are accessing the property sex it’s not found on the object but it is found on the object’s prototype. Hence it returns male.

    Then when you assign a new value to sex it creates a new property on the given object instead of changing the property on the object’s prototype because JavaScript is a dynamic language.

    This is fine since the new value shadows the old value. That’s the way prototypal inheritance works.

    If you explicitly want to update the property on the prototype instead of the given object I suggest you create propeties on your objects as follows:

    function defineProperty(object, name, value) {
        Object.defineProperty(object, name, {
            value: value,
            writable: true,
            enumerable: true
        });
    }
    
    function extendObject(object) {
        var extendedObject = Object.create(object);
    
        Object.keys(object).forEach(function (key) {
            Object.defineProperty(extendedObject, key, {
                set: function (value) {
                    object[key] = value;
                },
                enumerable: true
            });
        });
    
        return extendedObject;
    }
    
    var man = Object.create(null);
    defineProperty(man, "sex", "male");
    
    var yehuda = extendObject(man);
    defineProperty(yehuda, "firstName", "Yehuda");
    defineProperty(yehuda, "lastName", "Katz");
    

    This should solve your problem. To know more about prototypal inheritance read this answer.

    Edit: I suggest you do not try to change the values of the prototype. This is because many objects may have the same prototype. Changing any value on the prototype means that the change will be reflected on all the objects depending on it. Unless that is what you want to achieve I suggest you make do with shadowing properties on the prototypal chain.

    Note: You can delete properties on objects. If the property you deleted was shadowing another property then it will shadowed property will be used the next time you access the same property name.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

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.