It's not clear that your application requires the cloning of a function, but there is a way to do that.
I've included a cloning function below.
function cloneFunction (fn) {
let fnStr = fn.toString();
let fnClone = new Function('return ' + fnStr);
return fnClone();
}
let myFunc = function (a) { return a * 2; }
console.log('myFunc(1):', myFunc(1));
let mfClone = cloneFunction(myFunc);
console.log('mfClone(2):', mfClone(2));
Is this really a function cloning problem?
Is the issue truly a problem with cloning functions? Based on the information provided in the question, there seems to be a more fundamental problem.
The reported trouble item is scale
:
scale: d3.scaleSequential(d3.interpolateGreens)
This expression sets the value of scale
to the following function:
function scale(x) {
return isNaN((x = +x))
? unknown
: interpolator(
k10 === 0
? 0.5
: ((x = (transform(x) - t0) * k10),
clamp ? Math.max(0, Math.min(1, x)) : x)
);
}
Then, at some later point, scale
is invoked in this statement:
nestedObj[key].scale().domain([0,number)
which includes a call to scale()
with no argument. That returns undefined
, so scale().domain([0,number)
produces a TypeError.
Here's a code snippet that shows the problem in action:
const result = document.getElementById('result');
function emitData() {
let scale = d3.scaleSequential(d3.interpolateGreens);
result.innerHTML = 'scale.toString():\n' + '"' + scale.toString() +
'"\n\n';
result.innerHTML += 'scale() returns:\n' + scale();
result.innerHTML += '\n\nscale().domain([0,1]) returns:\n';
try {
let test = scale().domain([0,1]);
result.innerHTML += test;
} catch (e) {
result.innerHTML += '"' + e.toString() + '"';
}
}
pre {
padding: 0.5rem;
border: 1px solid #aaa;
width: 95%;
min-height: 6rem;
white-space: pre;
overflow-x: auto;
display: inline-block;
}
<p id="client">Testing "scale = d3.scaleSequential(d3.interpolateGreens)"</p>
<button onclick="emitData()">Run Test</button><br/>
<pre id="result"></pre>
<script src="https://d3js.org/d3-array.v2.js"></script>
<script src="https://d3js.org/d3-color.v1.js"></script>
<script src="https://d3js.org/d3-format.v1.js"></script>
<script src="https://d3js.org/d3-interpolate.v1.js"></script>
<script src="//d3js.org/d3-scale-chromatic.v0.3.js"></script>
<script src="https://d3js.org/d3-time.v1.js"></script>
<script src="https://d3js.org/d3-time-format.v2.js"></script>
<script src="https://d3js.org/d3-scale.v3.js"></script>