ES6 arrow functions not working on the prototype?

Jonathan Lucas

When ES6 Arrow functions don't seem to work for assigning a function to an object with prototype.object. Consider the following examples:

function Animal(name, type){
 this.name = name;
  this.type = type;
  this.toString = () => `${this.name} is a ${this.type}`;

}
var myDog = new Animal('Max', 'Dog');
console.log(myDog.toString()); //Max is a Dog

Using the arrow function explicitly in the object definition works, but using the arrow functions with the Object.prototype syntax does not:

function Animal2(name, type){
  this.name = name;
  this.type = type;
}
Animal2.prototype.toString = () => `${this.name} is a ${this.type}`;

var myPet2 = new Animal2('Noah', 'cat');
console.log(myPet2.toString()); //is a undefined

Just as a proof of concept, using the Template string syntax with Object.prototype syntax does work:

function Animal3(name, type){
  this.name = name;
  this.type = type;
}
Animal3.prototype.toString = function(){ return `${this.name} is a ${this.type}`;}

var myPet3 = new Animal3('Joey', 'Kangaroo');
console.log(myPet3.toString()); //Joey is a Kangaroo

Am I missing something obvious? I feel that example 2 should work logically, but I am puzzled by the output. I'm guessing it is a scoping issue, but I am thrown off by the output 'is a undefined'.

ES6 Fiddle

Amit

Arrow functions provide a lexical this. It uses the this that is available at the time the function is evaluated.

It is logically equivalent to (the following isn't valid code since you can't have a variable named this):

(function(this){
   // code that uses "this"
 })(this)

In your 1st example the arrow function is within the constructor, and this points to the newly generated instance.

In your 3rd example, an arrow function isn't used and standard this behavior works as always (the this in the function scope).

In your 2nd example, you use an arrow function but at the scope it's evaluated, this is global / undefined.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Using _ (underscore) variable with arrow functions in ES6/Typescript

From Java

Is it possible to export Arrow functions in ES6/7?

From Java

When should I use `return` in es6 Arrow Functions?

From Java

Methods in ES6 objects: using arrow functions

From Dev

Immediate function using JavaScript ES6 arrow functions

From Dev

What does "this" refer to in arrow functions in ES6?

From Dev

When should I use a return statement in ES6 arrow functions

From Dev

Explain effect of ES6 class constructor and arrow functions

From Dev

Compiling ES6 arrow functions to Es5 using Babel.js

From Dev

Official information on `arguments` in ES6 Arrow functions?

From Dev

How is .call supposed to work for ES6 Arrow Functions (according to the standards)?

From Dev

How to use ES6 arrow functions with promise binding (bluebird)

From Dev

ES6 in NodeJS: Arrow functions in object literal, what is the returned 'this' value?

From Dev

Are ES6 arrow functions incompatible with Angular?

From Dev

ES6 arrow functions

From Dev

es6 arrow functions debugger statement

From Dev

Why is `this` not working in an ES6 arrow function?

From Dev

How to run ES6 code with arrow functions in Safari?

From Dev

Why we have to wrap throw with brackets in es6 arrow functions

From Dev

jQuery .each() function with ES6 arrow functions

From Dev

javascript es6 double arrow functions

From Dev

Meteor ES6 fat arrow function and `this` in onCreated not working

From Dev

ECMAScript 6 arrow functions

From Dev

Using ES6 Arrow Functions in Node 0.11 w/ Foo.prototype

From Dev

Jump to ES6 arrow functions in Sublime text editor

From Dev

Can't use fat arrow functions (ES6) in react

From Dev

This not working as expected in nested arrow functions in ES6

From Dev

React / ES6 - Why calling a function inside another only works with es6 arrow functions?

From Dev

Argument types in functions versus in arrow functions in ES6

Related Related

  1. 1

    Using _ (underscore) variable with arrow functions in ES6/Typescript

  2. 2

    Is it possible to export Arrow functions in ES6/7?

  3. 3

    When should I use `return` in es6 Arrow Functions?

  4. 4

    Methods in ES6 objects: using arrow functions

  5. 5

    Immediate function using JavaScript ES6 arrow functions

  6. 6

    What does "this" refer to in arrow functions in ES6?

  7. 7

    When should I use a return statement in ES6 arrow functions

  8. 8

    Explain effect of ES6 class constructor and arrow functions

  9. 9

    Compiling ES6 arrow functions to Es5 using Babel.js

  10. 10

    Official information on `arguments` in ES6 Arrow functions?

  11. 11

    How is .call supposed to work for ES6 Arrow Functions (according to the standards)?

  12. 12

    How to use ES6 arrow functions with promise binding (bluebird)

  13. 13

    ES6 in NodeJS: Arrow functions in object literal, what is the returned 'this' value?

  14. 14

    Are ES6 arrow functions incompatible with Angular?

  15. 15

    ES6 arrow functions

  16. 16

    es6 arrow functions debugger statement

  17. 17

    Why is `this` not working in an ES6 arrow function?

  18. 18

    How to run ES6 code with arrow functions in Safari?

  19. 19

    Why we have to wrap throw with brackets in es6 arrow functions

  20. 20

    jQuery .each() function with ES6 arrow functions

  21. 21

    javascript es6 double arrow functions

  22. 22

    Meteor ES6 fat arrow function and `this` in onCreated not working

  23. 23

    ECMAScript 6 arrow functions

  24. 24

    Using ES6 Arrow Functions in Node 0.11 w/ Foo.prototype

  25. 25

    Jump to ES6 arrow functions in Sublime text editor

  26. 26

    Can't use fat arrow functions (ES6) in react

  27. 27

    This not working as expected in nested arrow functions in ES6

  28. 28

    React / ES6 - Why calling a function inside another only works with es6 arrow functions?

  29. 29

    Argument types in functions versus in arrow functions in ES6

HotTag

Archive