1

I wanna use a method like below with arrow functions:

clickElement = element => element.click();

Instead of a code like below:

clickElement(element) {
    return element.click();
}

But i am getting a syntax error under the first equal sign. How can i resolve that and use arrow function? Could anyone please advice. Thanks in advance!

  • 1
    Where is this function written? Inside a class, object, another function? – adiga Jan 06 '20 at 09:53
  • 3
    use like this: const clickElement = element => element.click(); – Sunil tc Jan 06 '20 at 09:54
  • 1
    @adiga - the function is inside a class – Aravindan K Jan 06 '20 at 09:59
  • @Sunil tc - When i use 'const', i am getting the error as - "'const' can only be used in a .ts file". I am not using Typescript. – Aravindan K Jan 06 '20 at 10:01
  • 1
    Why do you want to convert to an arrow function? `clickElement(element) {}` will add the function to the class prototype. Whereas `clickElement = element => element.click()` will create a [class field](https://github.com/tc39/proposal-class-fields). You need to enable the feature since it is not widely supported as of now. Please go through this: [How to use arrow functions (public class fields) as class methods?](https://stackoverflow.com/q/31362292) – adiga Jan 06 '20 at 10:04
  • Can you add complete code to know whether it's inside any class or fucntion ? – Sunil tc Jan 06 '20 at 10:04
  • What IDE are you using? I think it's a lint error, try to see if your eslint/jslint is set correctly(probably has been set below ES2015). – Hao Wu Jan 06 '20 at 10:10
  • You still have this issue? – Bharath Kumar S Jan 09 '20 at 10:17

2 Answers2

0

Assign the function to a variable.

const clickElement = element => element.click();
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
Sunil tc
  • 2,482
  • 4
  • 19
  • 46
  • `clickElement` doesn't have a `function` keyword. So, this is either inside a class or an object where `const` will not work – adiga Jan 06 '20 at 09:59
  • @adiga Arrow function does not need function keyword. – Sunil tc Jan 06 '20 at 10:01
  • I meant OP's working code `clickElement(element) { return element.click(); }` – adiga Jan 06 '20 at 10:02
  • I just wanted to implement arrow function to simplify the code. How can i do that? When i use 'const', i am getting the error as - "'const' can only be used in a .ts file". I am not using Typescript. – Aravindan K Jan 06 '20 at 10:08
  • @AravindanK they are completely different things. Arrow function will be added to each instance of the class. With your existing syntax, it will be added only to `yourClass.prototype.clickElement`. If you still want to use arrow, you need to enable the feature: [How to use arrow functions (public class fields) as class methods?](https://stackoverflow.com/questions/31362292) – adiga Jan 06 '20 at 10:11
  • you just need variable to keep the function: var clickElement = element => element.click(); – Sunil tc Jan 06 '20 at 10:11
  • @AravindanK Can you give some more information to know exactly whats the issue? – Sunil tc Jan 06 '20 at 10:12
  • @Suniltc this has nothing to do with adding `const` or `var` – adiga Jan 06 '20 at 10:13
  • Thanks @adiga i will check the link that you have shared. – Aravindan K Jan 06 '20 at 10:25
  • Even after using - npm install --save-dev babel-plugin-transform-class-properties and npm install --save-dev @babel/plugin-proposal-class-properties, i am not able to use arrow functions – Aravindan K Jan 06 '20 at 10:56
  • Do i really need to use arrows, or this is fine? clickElement(element) { return element.click(); } – Aravindan K Jan 06 '20 at 10:57
  • @AravindanK that is fine and I'd actually recommend it over arrow. If you add class fields, it will be added to each instance. – adiga Jan 06 '20 at 11:38
-1

try it like this?

const clickElement = (element) => element.click();

Call it like this:

clickElement(someElement);
Joaquin Casco
  • 704
  • 5
  • 14