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
Copy file name to clipboardExpand all lines: questions/what-advantage-is-there-for-using-the-arrow-syntax-for-a-method-in-a-constructor/en-US.mdx
+7-5Lines changed: 7 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ The main advantage of using an arrow function as a method inside a constructor i
8
8
9
9
For example, let's say we have a `Person` constructor that takes a first name as an argument has two methods to `console.log()` that name, one as a regular function and one as an arrow function:
Arrow functions cannot be used as constructors and will throw an error when used with the `new` keyword.
88
90
89
-
```js
91
+
```js live
90
92
constFoo= () => {};
91
93
constfoo=newFoo(); // TypeError: Foo is not a constructor
92
94
```
@@ -104,7 +106,7 @@ arrowFunction(1, 2, 3);
104
106
105
107
Since arrow functions do not have their own `this`, they are not suitable for defining methods in an object. Traditional function expressions or function declarations should be used instead.
106
108
107
-
```js
109
+
```js live
108
110
constobj= {
109
111
value:42,
110
112
getValue: () =>this.value, // `this` does not refer to `obj`
@@ -119,7 +121,7 @@ One of the most notable features of arrow functions is their behavior with `this
0 commit comments