3 ms·
It's been a little while since I wrote JavaScript. Aren't Function objects immutable? Would it not satisfy the desired behavior of clone to reference the same
by dap 3y ago
It's been a little while since I wrote JavaScript. Aren't Function objects immutable? Would it not satisfy the desired behavior of clone to reference the same function (not copy it) in the new object?
- aragonite 3y agoFunction objects are mutable in the sense that you can define new properties for them or assign new values to (writable) existing properties. This is sometimes used to simulate static variables: function f() { f.count ??= 1; console.log(f.count++); } f(); // Outputs: 1 f(); // Outputs: 2 f(); // Outputs: 3
- eyelidlessness 3y agoEven if functions were immutable as objects, they can reference anything within their containing scope, including arbitrary dynamic variables in outer scopes and even values from other modules (whose bindings are “live”). They may also have their “this” bound to arbitrary objects from any scope at all.