You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm sorry that preserving this context might not be possible under current design of safe-touch.
Let me explain:
As you already know, if you want to retrieve property, apply the wrapped value as a function, e.g. safeTouch(a).b(). There is an explicit function call. In fact, every step in the property chain is a function call.
Then let's add such function to the snippet you provided:
function test() {
return a.b
}
test() // undefined
Now we have proved that function returned from other scope could not preserve this context, even if it tried to chain.
Some people might ask - what about use bind/apply/call?
There does exist other libs that was implemented in that way, but they will create extra function, which is against one of design principles of safe-touch, i.e. safeTouch(original).key() === original.key.
This principle keeps safe-touch pure and simple.
But I have to admit: one the original goal of safe-touch was, if all property exists, user should be able to touch the object as if it was not wrapped by safe-touch. That is, safeTouch(a).b()() === 1 in the case of your snippet.
The text was updated successfully, but these errors were encountered: