Hacker News new | past | comments | ask | show | jobs | submit login

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?



Even 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.


Function 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




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: