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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:52:27+00:00 2026-05-27T14:52:27+00:00

How do I copy an object and it’s prototype chain without calling its constructor

  • 0

How do I copy an object and it’s prototype chain without calling its constructor function?

In other words, what would the function dup look like in the following example?

class Animal
  @sleep: -> console.log('sleep')
  wake: -> console.log('wake')
end
class Cat extends Animal
  constructor: ->
    super
    console.log('create')

  attack: ->
    console.log('attack')
end

cat = new Cat()         #> create
cat.constructor.sleep() #> sleep
cat.wake()              #> wake
cat.attack()            #> attack

dup = (obj) ->
  # what magic would give me an effective copy without
  # calling the Cat constructor function again.

cat2 = dup(cat)          #> nothing is printed!
cat2.constructor.sleep() #> sleep
cat2.wake()              #> wake
cat2.attack()            #> attack

As much as it pains me to look at, here’s a jsfiddle of the example.

I’d also need the properties despite only using functions in my example.

  • 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-27T14:52:28+00:00Added an answer on May 27, 2026 at 2:52 pm
    function dup(o) {
        return Object.create(
            Object.getPrototypeOf(o),
            Object.getOwnPropertyDescriptors(o)
        );
    }
    

    Which relies on the ES6 Object.getOwnPropertyDescriptors. You can emulate it. Taken from pd

    function getOwnPropertyDescriptors(object) {
        var keys = Object.getOwnPropertyNames(object),
            returnObj = {};
    
        keys.forEach(getPropertyDescriptor);
    
        return returnObj;
    
        function getPropertyDescriptor(key) {
            var pd = Object.getOwnPropertyDescriptor(object, key);
            returnObj[key] = pd;
        }
    }
    Object.getOwnPropertyDescriptors = getOwnPropertyDescriptors;
    

    Live Example

    Converting this into coffeescript is left as an exercise for the user. Also note that dup shallow copies own properties.

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

Sidebar

Related Questions

I would like to copy one object to another object, and the fields with
It's a bit difficult to implement a deep object copy function. What steps you
Are Php function parameters passed as references to object or as copy of object?
I need to deep copy a custom object that has objects of its own.
I would like to ask about PHP clone / copy object to $this variable.
Possible Duplicate: How do I copy an object in Java? I've got a function
I need to copy an object that has a pretty deep hierarchy of member
I'm trying to make a deep copy of an object, including a GregorianCalendar instance.
I want to copy values from one object to another object. Something similar to
The object inherits from NSObject. Is there a method to create a copy of

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.