question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cloning copies prototype properties onto the object

See original GitHub issue
function Test () {}
Test.prototype.val = 42;

console.log(klona(new Test())); // { val: 42 }
console.log(klona(new Test()).__proto__); // {}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
lukeedcommented, Jul 4, 2020

Hey @evilebottnawi – I’m not sure what you’re switching from, but I’m guessing it’s clone-deep. Since you said it’s a blocker, I just wanted to let you know that clone-deep never actually clones a custom class instance unless you explicitly tell it to. By default, it returned the original instance. Only lodash – and now klona – return a new fresh instance of the custom class by default.

You can see this here:

const clone = require('clone-deep');
const klona = require('klona');

function Test () {}
Test.prototype.val = 42;

function run(name, lib) {
	let input = new Test();
	let output = lib(input);

	console.log('[%s] before:', name, output.val);
	input.val++; // modify source
	console.log('[%s] after:', name, output.val);
}

run('clone-deep', clone);
run('clone-deep (instance)', foo => clone(foo, true));
run('klona', klona);

//=> [clone-deep] before: 42
//=> [clone-deep] after: 43
//=> [clone-deep (instance)] before: 42
//=> [clone-deep (instance)] after: 42
//=> [klona] before: 42
//=> [klona] after: 42
1reaction
alexander-akaitcommented, Jul 3, 2020

@lukeed can we fix it? Blocker for fully migrate on klona

Read more comments on GitHub >

github_iconTop Results From Across the Web

JavaScript cloned object loses its prototype functions
The only way to really "clone" an object is to set its [[Prototype]] to the same object as the original, then copy the...
Read more >
Prototype - Object clone() Method - Tutorialspoint
Prototype - Object clone() Method, This method clones the passed object using the shallow copy. It copies all the original's properties to the...
Read more >
Cloning JavaScript Object with Object.create | by Mayank Gupta
When we create Object with Object.create, an empty Object is created and the __proto__ object is mapped to the copied Object. When __proto__...
Read more >
Prototype Pattern. Creates new objects by copying an…
Swift will copy the value of the prototype and use it to create a clone. struct having reference types are shallow copied, they...
Read more >
How to copy objects in JavaScript: A complete guide
A complete guide to copying objects in JavaScript: shallow copy, deep copy, assigning, merging, and structured cloning.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found