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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:46:57+00:00 2026-05-23T16:46:57+00:00

I have a Point function Point(x, y) { this.x = x; this.y = y;

  • 0

I have a Point

function Point(x, y) {
    this.x = x;
    this.y = y;
};

As you see, it’s mutable. So I can change it properties, like

 var p = new Point(2, 3);
 p.x = 6;

I want to add clone method so that expected behavior would be

 var p1 = new Point(2, 3);
 var p2 = p1.clone();
 p1.x = 6;

 assert p1 != p2;     //first assertion. pseudocode.
 assert p2.x == 2;    //second assertion. pseudocode.

For implementing clone() I rewrite Point in next way

function Point(x, y) {
    this.x = x;
    this.y = y;
    this.clone = function () {
        function TrickyConstructor() {
        }
        TrickyConstructor.prototype = this;
        return new TrickyConstructor();
    };
};

But second assertion fails for my implementation. How should I reimplement 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-05-23T16:46:58+00:00Added an answer on May 23, 2026 at 4:46 pm

    If the properties are only x and y, I would do this:

    function Point(x, y) {
        this.x = x;
        this.y = y;
    };
    
    Point.prototype.clone = function() {
        return new Point(this.x, this.y);
    }
    

    Note that I attach the clone method to Point.prototype. This is important for the next method to work:

    If not, you would have to create a new instance and maybe copy all properties to the new instance:

    Point.prototype.clone = function() {
        var clone = new Point(this.x, this.y);
        for(var prop in this) {
            if(this.hasOwnProperty(prop)) {
                clone[prop] = this[prop];
            }
        }
        return clone;
    }
    

    but this will not deep copy properties. This only works for primitive values.

    If you really want to deep copy properties, this can get much more complex. Luckily, this has already been asked before: How to Deep clone in javascript


    Explanation of why your clone method does not work:

    The prototype chain of p2 will look like this:

     +-----------+      +-----------+
     |Instance p2|      |Instance p1|
     |           |----->|x=2        |
     |           |      |y=3        |
     +-----------+      +-----------+
    

    so if you set p1.x = 6 it will be:

     +-----------+      +-----------+
     |Instance p2|      |Instance p1|
     |           |----->|x=6        |
     |           |      |y=3        |
     +-----------+      +-----------+
    

    As long as p2 has no own x or y properties, they will always refer to the ones of the prototype which happens to be p1.

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

Sidebar

Related Questions

I have a function defined like this: public static void ShowAbout(Point location, bool stripSystemAssemblies
I have written a function to print database table to an array like this
How can I set a break point in a javascript function and have the
Heyy there, i have for example a view function like this: def profile_view(request, id):
I can't show a live example at this point, but i don't see how
I have this function which is imported directly in the HTML function include(filename){ var
I have this function: function disableDiv(divId, action){ var divId = byId(divId); if(action==true){ divId.style.display='none'; }
say I have this class: class animal { function noise() { print 'woof'; }
Is there any point to freeing memory in an atexit() function? I have a
I have a pointer which points to a function. I would like to: if

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.