|
|
|
|
|
by ludwigvan
4699 days ago
|
|
> If you define this.sayHi in the constructor, then each Person you create gets its own copy of the function, making every Person heavier in memory. Does anyone know whether this is true for more sophisticated JS engines like v8? I seem to recall reading something like that, that it detects that it is the same function, but I might be misremembering it. |
|
* That the function in question never accesses anything in its closure that could be different for each construction
* That hasOwnProperty() is never called on the object to check which way the method is declared (alternatively, remember that it had done this optimization, and spoof the return value)
* That the function is never used as a constructor. Otherwise, Person could update sayHi's prototype and wind up changing its effects for all Persons, where it was supposed to create a customized inner class.
And if you think any of these are easy, remember that you can get a pointer to that function via the following: