Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Sample {
- constructor() {
- this.prefix = 'this is';
- this.list = [1, 2, 3, 4, 5];
- }
- mapList() {
- return this.list.map(function(x) {
- return this.prefix + x;
- });
- }
- }
- const obj = new Sample();
- obj.mapList(); // Cannot read property 'prefix' of undefined
- function Sample() { ... }
- Sample.prototype.mapList = function() {
- return this.list.map(function(x) {
- return this.prefix + x
- });
- }
- // 'this' is undefined when mapList() is called
Add Comment
Please, Sign In to add comment