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

The Archive Base Latest Questions

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

I have some test class TestKlass = (function() { function TestKlass(value) { this.value =

  • 0

I have some test class

TestKlass = (function() {

    function TestKlass(value) {
      this.value = value != null ? value : 'hello world';
      console.log(this.value);
    }

    return TestKlass;

})();

x = new TestKlass;
x instanceof TestKlass; (gives true)

I have new empty object

y = {}
y instanceof Object

Can I find any ways to set any properties for y, something like this

y.__proto__ = x.__proto__
y.constructor.prototype = x.constructor.prototype

for to have this result

y instanceof TestKlass => true

====================================================

UPD:

So. My main aim – it’s to create CLONE function. Now my solution works for me. Please look at this code:

JavaScript JS object clone

Object._clone = function(obj) {
  var clone, property, value;
  if (!obj || typeof obj !== 'object') {
    return obj;
  }
  clone = typeof obj.pop === 'function' ? [] : {};
  clone.__proto__ = obj.__proto__;
  for (property in obj) {
    if (obj.hasOwnProperty(property)) {
      value = obj.property;
      if (value && typeof value === 'object') {
        clone[property] = Object._clone(value);
      } else {
        clone[property] = obj[property];
      }
    }
  }
  return clone;
};

CoffeeScript JS object clone

# Object clone
Object._clone = (obj) ->
  return obj if not obj or typeof(obj) isnt 'object'
  clone = if typeof(obj.pop) is 'function' then [] else {}

  # deprecated, but need for instanceof method
  clone.__proto__ = obj.__proto__

  for property of obj
    if obj.hasOwnProperty property
      # clone properties
      value = obj.property
      if value and typeof(value) is 'object'
        clone[property] = Object._clone(value)
      else
        clone[property] = obj[property]

  clone

Now you can try to do that

A = new TestKlass
B = Object._clone(A)
B instanceof TestKlass => true

It’s work fine with Moz FF 13. But I think it’s not cross-browser. and proto is deprecated.

I think there is no universal solution. But maybe It’s will be helpful for somebody.

  • 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:30:18+00:00Added an answer on June 6, 2026 at 4:30 pm

    Perhaps you should read the following answer first. What you are trying to achieve is really simple. However before I explain let’s look at your solutions:

    Solution 1

    y.__proto__ = x.__proto__;
    

    I wouldn’t recommend doing this as the __proto__ property is now deprecated. The above code won’t work in Rhino.

    Solution 2

    y.constructor.prototype = x.constructor.prototype;
    

    This is a very bad solution. The reason is that y.constructor is actually y.__proto__.constructor and since y is an object y.__proto__ is the same as Object.prototype. This means that y.constructor is the same as Object and you’re essentially changing the prototype property of Object. This could potentially break other code on your page. I strongly discourage this practice.

    Solution 3 (my solution)

    What you want to do is simply change the internal [[proto]] property of an object. Since there’s no cross platform way to do so we need to employ a hack. A simple solution would be to create a new object with the desired __proto__ property and set getters and setters on it to change the original object. It would be as follows:

    function setPrototypeOf(object, prototype) {
        var constructor = function () {
            for (var key in object) {
                if (object.hasOwnProperty(key)) {
                    (function (key) {
                        Object.defineProperty(this, key, {
                            get: function () {
                                return object[key];
                            },
                            set: function (value) {
                                object[key] = value;
                            },
                            enumerable: true
                        });
                    }).call(this, key);
                }
            }
        };
    
        constructor.prototype = prototype;
    
        return new constructor;
    }
    

    After this all you need to do is:

    y = setPrototypeOf(y, TestKlass.prototype);
    

    Here is a working fiddle.

    Edit:

    The problem with your clone function is that it only clones objects and arrays. You forgot to account for functions which are also passed by reference. Thus when you clone an object which has methods which close over the internal state of the object, the object and its clone will share the same internal state. See the following fiddle:

    function Value(value) {
        this.get = function () {
            alert(value);
        };
        this.set = function (newValue) {
            value = newValue;
        };
    }
    
    var a = new Value(5);
    var b = Object._clone(a);
    b.set(10);                // setting b to 10 also sets a to 10
    a.get();                  // alerts 10, should alert 5
    

    There’s absolutely no way in JavaScript to clone the internal state of an object so cloning objects and arrays in JavaScript will only work for objects which do not expose closures as methods.

    To know more about the problems with cloning objects in JavaScript read this answer. Click on the following link to read John Resig’s answer.

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

Sidebar

Related Questions

Suppose I have the following html: This a test of <code>some code</code>. <div class='highlight'>
I have some classes(call it Class A) which I would like to unit test
I am writing some unit test and I have a unit testing base class
Say I have this Ruby code in test.rb module MyModule class TestClassA end class
I have some test cases. The test cases rely on data which takes time
I have some useful wpf buttons to test some functionality. It would be good
I have to test some things on a project which has been developed by
I have some code that attempts to test whether my application is running with
I have some .csv files which I'm using as part of a test bench.
I have some code in my error handler I need to test against a

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.