JS overshadow - function

by matox

JavaScript

'use strict';

function Book(title) {
  this.title = title;
  this.pages = [1, 2, 3, 4, 5];
}

Book.prototype.print = function() {
	console.log(this);
  this.pages.forEach(function(page) { // B
  	console.log(this);
    console.log(this.title, page);
  });
}

const b = new Book('JS for dummies');
b.print();

/*
	Just an ES5 way of writing https://jsfiddle.net/matox/0jnezvug/7/

	class Book {	// A
    constructor(title) {
      this.title = title;
      this.pages = [1, 2, 3, 4, 5];
    }

    print() {
      this.pages.forEach(function(page) {		// B
        console.log(this.title, page);
      });
    }
  }
*/