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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:42:59+00:00 2026-06-11T13:42:59+00:00

Hi I am relatively new to jQuery and JavaScript and have a question regarding

  • 0

Hi I am relatively new to jQuery and JavaScript and have a question regarding the functionality of $.extend.
I am using extend to overwrite the prototype of a class that extends a “base class”.
Lets call the base Parent and the the children/extend classes A and B.
Now when I use $.extend(true, A.prototype, {someObject: {extraKey: 'value'}}); it seems also the Prototype of Parent is changed regarding the contents of someObject, resulting in really odd behavior if several objects/classes inherit from parents. Also this only happens if the “deep” extending is used.

I managed to avoid this problem by extending an empty object with the respective prototype and then using the return value of extend as the new prototype. E.g. A.prototype = $.extend(true, {}, A.prototype, {someObject: {extraKey: 'value'}});

To show the problem I created this small jsfiddle: http://jsfiddle.net/x8UtF/12/

The actual question: Is there a way to get around the empty objects and stuff to just go extend(true, X.prototype, {});

  • 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-11T13:43:00+00:00Added an answer on June 11, 2026 at 1:43 pm

    So, when you do this:

    $.extend( true, A.prototype, {
        someObject: {Aone: 1, Atwo: 2}
    });
    

    this will happen as a result:

    1. The properties "Aone", and "Atwo" will be assigned to Parent.prototype.someObject, instead of A.prototype.someObject (which at that point doesn’t event exist).
    2. The property A.prototype.someObject will be created and its value will be set to Parent.prototype.someObject.

    So, both A.prototype.someObject, and Parent.prototype.someObject will refer to the same object:

    A.prototype.someObject === Parent.prototype.someObject // true
    

    This is, of course, not what you want.


    That being said, I’m concerned that your pattern is not good. A.prototype is supposed to inherit from Parent.prototype, yet you copy the values from Parent.prototype.someObject to A.prototype.someObject.

    Also, your current solution:

    A.prototype = $.extend( true, {}, A.prototype, {
        someObject: {Aone: 1, Atwo: 2}
    });
    

    is not intuitive. It’s not easy to decipher what the code is doing. I recommend re-evaluating that pattern. (You can ask Stack Overflow for help, of course.)

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

Sidebar

Related Questions

I have a relatively simple example of an MVC page that is using jQuery
Im relatively new to the Android world but I have a quick question. I
I am very new to javascript and ajax/jquery and have been working on trying
I'm still relatively new to javascript and jQuery and was just wondering this. Suppose
I'm using jQuery tablesorter plugin. I'm relatively new to jQuery and need help from
I'm relatively new to javascript so please hold it against me. I have a
i'm relatively new to jquery and javascript and am trying to pass a unique
Being relatively new to javascript, AJAX and jQuery, I want to post 3 values
I'm relatively new to javascript and am building a web page. I'm using the
I am relatively new to implementing JQuery throughout an entire system, and I am

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.