bartolaus
bartolaus

Reputation: 63

problems with rebinding function when cloning an object

I have an object with properties of type object and one of these contains functions, that are bound to the "base" object. When i now try to clone that "base" object, i clone also every property of type object to ensure that there are no references to old data left and i also try to bind the mentioned functions to the new cloned object (because they use a this reference), but when i change some values in the clone and use one of these functions they seem to be still bound to the old "base" object.

Anyone got an idea why this is happening?

Upvotes: 1

Views: 30

Answers (1)

c-smile
c-smile

Reputation: 27470

If that's about attempts to "rebind" functions that are bound by Function.prototype.bind() call then you are out of luck.

The bind() creates new function-wrapper - closure around original function and its this. As with any other "closured" variables - you simply have no access to them and so cannot change them from outside. And so you cannot "rebind" bound function.

Check: Can you rebind a rebound function using `bind`

Upvotes: 1

Related Questions