Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
221,293 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