3 ms·
Are you complaining that passing around a function reference doesn't maintain the `this` reference? That's... uh... just how Javascript works. That's how func
by acemarke 10y ago
Are you complaining that passing around a function reference doesn't maintain the `this` reference? That's... uh... just how Javascript works. That's how functions behave, that's how prototypal inheritance works, and the class syntax maintained that behavior for backwards compatibility. That has nothing to do with Webpack, Babel, React, or any other tool. That's part of the language syntax.
There's several strategies for ensuring that `this` in a function refers to a particular class instance, particularly in a React app. The basic approaches are re-binding the function every time you re-render; binding each function once in the constructor; using a decorator or utility method to bind each function once; and using the Stage 2 Class Properties syntax and writing the function using the arrow syntax. See http://reactkungfu.com/2015/07/why-and-how-to-bind-methods-in-your-react-component-classes/ http://reactkungfu.com/2015/07/why-and-how-to-bind-methods-i... for an overview of the approaches.
- milesrout 10y agoIt's how it works, and it's wrong. It's incorrect. It's badly designed. It should be fixed. Let me write `'use sanity;'` at the top of a function (like `'use strict';`) and then let me use sane lexically scoped semantics.
- Terr_ 10y agoI completely agree with your frustration, but even inside the a "sane block" the are cases where you'll need the existing behavior, and backwards compatibility... Ugh. Almost easier to make completely new keywords that allow you to designate where a special-scope begins and a "this"-like keyword that behaves a little differently.