[TIMOB-25807] Hyperloop - ES6: Cannot find scoped "this" methods anymore
GitHub Issue | n/a |
---|---|
Type | Bug |
Priority | Medium |
Status | Closed |
Resolution | Invalid |
Resolution Date | 2018-03-21T10:00:52.000+0000 |
Affected Version/s | Hyperloop 3.1.0 |
Fix Version/s | n/a |
Components | Hyperloop |
Labels | n/a |
Reporter | Hans Knöchel |
Assignee | Unknown |
Created | 2018-02-24T19:05:01.000+0000 |
Updated | 2018-08-06T17:37:03.000+0000 |
Description
When using ES6 in Hyperloop methods, e.g.
var ButtonDelegate = Hyperloop.defineClass('ButtonDelegate', 'NSObject');
ButtonDelegate.addMethod({
selector: 'buttonPressed:',
instance: true,
arguments: ['UIButton'],
callback: (sender) => {
if (this.buttonPressed) {
// ^-- Here is the issue - it cannot find the declared method
this.buttonPressed(sender);
}
}
});
var delegate = new ButtonDelegate();
// Not found when being used in "Hyperloop.addMethod"
delegate.buttonPressed = function(sender) {
alert('Button pressed!');
};
the this.buttonPressed
method cannot be found, although declared. We use this pattern many times, to assign methods to instances afterwards. If there is a better approach, we should replace the usages in the hyperloop-examples sample app.
*EDIT*: It only seems to be an issue if the delegate is in the same file as its usage. It works fine for other delegates / sub classes like [here](https://github.com/appcelerator/hyperloop-examples/blob/master/app/lib/ios/subclasses/tableviewdatasourcedelegate.js#L9).
[~cwilliams] Not sure if this is an SDK- or Hyperloop- or code-design-related issue.
Resolving as
Invalid
as ES6 arrow functions have a different scope. To solve this, simply use non-Arrow functions and make use of the global / file-widethis
scope.Closing as invalid. If incorrect, please reopen.