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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:21:21+00:00 2026-06-06T16:21:21+00:00

I’m trying to make a copy function and add it to the object’s prototype.

  • 0

I’m trying to make a “copy” function and add it to the object’s prototype. I planned to recursively typecheck and assign properties to a new object and then return the object… But, there seems to be a problem, see this snippet of code:

Object.prototype.copy = function()
{
    for (prop in this)
    {
        console.log(prop); //Logs copy (the function)!!!
    }
}

x = {"a": 1};
y = x.copy();

As I’ve pointed out in the comment, I found this very weird behavior, but why is this happening? The copy function should be in Object.prototype, not in the instanced object itself! How do I fix it? Can I just set this.copy = undefined, and still rely on Object.prototype.copy?

This is the full code sample, as requested:

Object.prototype.copy = function()
{
    var object = this; //The object we are copying.
    var newObject = {}; //The object we will return.

    //Cycle through the properties of the object we are creating, copy them recursively.
    for (prop in object)
    {
        if (!Object.prototype.hasOwnProperty.call(this, prop) || object[prop] == null)
        {
            continue;
        }

        if (prop == "copy")
        {
            console.log("Well, blah."); //This never prints!
        }

        if (typeof(object[prop]) == "object" && !(object[prop] instanceof Array)) //If the object's property is another object...
        {
            newObject[prop] = object[prop].copy(); //Set the copy of it to the new object as well.
            console.log("1 --- " + prop); //This prints copy - two times! That defies logic!
        }
        else if (typeof(object[prop]) == "object") //If it's an array...
        {
            newObject[prop] = object[prop].slice(); //Do it in a nicer fashion.
            console.log("2 --- " + prop);
        }
        else //You're safe to copy it.
        {
            newObject[prop] = object[prop];
            console.log("3 --- " + prop + " --- " + object[prop]);
        }
    }

    return newObject;
}
  • 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-06T16:21:24+00:00Added an answer on June 6, 2026 at 4:21 pm

    There’s a method called “hasOwnProperty” that you can use:

     if (this.hasOwnProperty(prop)) { ... }
    

    If the function returns true the it’s a “direct” property on the object.

    If you fear that the “hasOwnProperty” method may be borked, you can do this:

    if (Object.prototype.hasOwnProperty.call(this, prop)) { ... }
    

    instead.

    Newer versions of JavaScript have fancier ways of examining and controlling objects.

    edit — also your updated code involves a problem that’ll bite due to the nested calls to “copy”: you didn’t declare “prop” with var, so after the call to copy an object, the value of “prop” will have changed! (Every call to “copy” shares the same variable, in other words.)

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
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
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
i got an object with contents of html markup in it, for example: string

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.