Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloning copies prototype properties onto the object #14

Closed
dwelle opened this issue Feb 10, 2020 · 5 comments
Closed

Cloning copies prototype properties onto the object #14

dwelle opened this issue Feb 10, 2020 · 5 comments

Comments

@dwelle
Copy link

dwelle commented Feb 10, 2020

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

console.log(klona(new Test())); // { val: 42 }
console.log(klona(new Test()).__proto__); // {}
@lukeed
Copy link
Owner

lukeed commented Feb 10, 2020

Thank you

This will be included in the default & upcoming "full" modes.

@alexander-akait
Copy link

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

@lukeed
Copy link
Owner

lukeed commented Jul 4, 2020

Yup, I have a solution to this now. Had to spend some time on it so that performance didn't take too much of a blow.

This will be included in 1.1.2 and then I will immediately begin moving to 2.0.0 with #13 in mind.

@lukeed lukeed closed this as completed in c873e9e Jul 4, 2020
@lukeed
Copy link
Owner

lukeed commented 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

@TrySound
Copy link

TrySound commented Jul 4, 2020

Awesome! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants