Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Introducing ES2015 The Cooler Parts of ES2015 Arrow Functions

JavaScript ES2015 new stuff

I am finding this example hard to read as in what happens first...

'use strict';

var Teacher = function (data) { this.name = data.name; this.greet = function (studentCnt) { console.log(studentCnt); let promise = new Promise(function (resolve, reject) { if (studentCnt === 0) { reject('Zero students in class'); } else { resolve(Hello to ${studentCnt} of my favorite students!); } }); return promise; } }

var Classroom = function (data) { this.subject = data.name; this.teacher = data.teacher; this.students = []; this.addStudent = function (data) { this.students.push(data); this.greet(); } this.greet = () => { this.teacher.greet(this.students.length).then( (function (classroom) { return function (greeting) { console.log(${classroom.teacher.name} says:, greeting); } })(this), function (err) { console.log(err); }) } }

var myTeacher = new Teacher({ name: 'James' }); var myClassroom = new Classroom({ name: 'The Future of JavaScript', teacher: myTeacher });

myClassroom.addStudent({ name: 'Dave' });

It looks like to me the first line is var myTeacher = new Teacher({ name: 'James' });

If this is the case then the Teacher object will be created

but this.greet = function (studentCnt)

studentCnt has no value...

2 Answers

Steven Parker
Steven Parker
229,670 Points

The name studentCnt is a parameter, not a variable.

When the method is called from within Classroom, it is given this.students.length as the actual argument. And since "Dave" is the first student added, it will have the value of 1 (one), so "James says: Hello to 1 of my favorite students!"

To make your posted code more readable, be sure to use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:

Hi yes thanks I was tired and not looking at the code properly the method greet is not called until later..I somehow assumed it was invoked earlier.. I still have to work out the promises and read more on objectss