Guest User

Untitled

a guest
Jan 24th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  1. class Sample {
  2. constructor() {
  3. this.prefix = 'this is';
  4. this.list = [1, 2, 3, 4, 5];
  5. }
  6. mapList() {
  7. return this.list.map(function(x) {
  8. return this.prefix + x;
  9. });
  10. }
  11. }
  12. const obj = new Sample();
  13. obj.mapList(); // Cannot read property 'prefix' of undefined
  14.  
  15. function Sample() { ... }
  16. Sample.prototype.mapList = function() {
  17. return this.list.map(function(x) {
  18. return this.prefix + x
  19. });
  20. }
  21. // 'this' is undefined when mapList() is called
Add Comment
Please, Sign In to add comment