sasaplus1 / deepcopy.js

deep copy data

Home Page:https://sasaplus1.github.io/deepcopy.js/benchmark

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Copied functions loses scope

are opened this issue · comments

commented

Copied function loses its scope and therefore breaks it. Example:

var a = [ 1, 2, 3 ];
var base = {};

base.fn = function() {
     console.log(a);
}
base.fn();

var copy = deepcopy(base);
copy.fn(); //ReferenceError: a is not defined

Any idea how to fix it?

Hi, @are-are-

I implemented deep copy for Function type at 6d8d672. I thought Function is executable object.
I will revert its changes at the next version, because I rethink saw your report.

If you are hurry, I recommend to use lodash.clonedeep.

commented

The issue with current implementation is that function is turned to string and then evaluated back to a function. Because of that it can throw a lot of unexpected errors. I resolved this issue in my project by doing return target; instead of return (new Function('return ${source}')());, although it doesn't have to work for everyone. I suggest adding some kind of option to disable default behavior.

OK. I'll think it over. Thanks.

I changed to shallow copy from when ver.1.0.0.