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

Methods not copied over when deep copying a class #29

Open
benslv opened this issue Mar 16, 2022 · 5 comments
Open

Methods not copied over when deep copying a class #29

benslv opened this issue Mar 16, 2022 · 5 comments

Comments

@benslv
Copy link

benslv commented Mar 16, 2022

I ran into a bit of an issue today trying to make a deep copy of a class I had. It turns out rfdc doesn't copy over class methods, only its attributes.

Not sure if this is a limitation in JS or a feature that could be added to the package?

I made a minimal reproduction of the issue here:

const clone = require("rfdc")({proto: true});

class TestClass {
	constructor() {
		this.name = "John";
	}

	sayHello() {
		console.log("Hello!");
	}
}

const test = new TestClass()
test.sayHello() // "Hello!"

const testClone = clone(test)
testClone.sayHello() // TypeError: testClone.sayHello is not a function
@hood
Copy link

hood commented Apr 11, 2022

Methods cannot be serialized, so they cannot be "cloned". This would be more of a JS "feature", and it won't happen anytime soon, if ever.

@TommyDew42
Copy link

It would be great if we can document this limitation in the proto session

@hood
Copy link

hood commented Oct 7, 2022

This is not something to document on rfdc's side IMHO. It's completely unrealistic to expect a data serialisation library to be able to serialise logic.

@trevor-vaughan
Copy link

The cloneDeep method in lodash could do it but I haven't found anything else that provides a similar capability (yet).

@trevor-vaughan
Copy link

Hmm....the clone library also seems to clone objects properly.

@hood Is RFDC a 'cloning' library or a 'serialization' library?

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