Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Trace What new Does

In this video, Mei stops spelling out every plant record by hand. She writes a Plant class in a new file, plant.js, in her plant-tracker folder, and makes Goldie and Moss with new.

You trace, step by step, what new does each time. For now, the plants carry only their data. A function that returns the record could do that too, and the class pays off in the next lesson, when the methods come back.

Key Points

  • A class starts with the keyword class and a name. By convention, class names start with a capital letter, as in Plant, so a class stands out from the objects it makes.
  • The constructor goes inside the class’s braces. It’s written like a method, but its name is fixed. The constructor holds the code that new runs. Its parameters are the values every plant needs. Each line of its body sets one property on this.
  • new Plant("Goldie", 5, 6) does three things, in order:
  • It makes a fresh, empty object.
  • It runs the constructor with this set to that object, and the values go to the constructor’s parameters. Each line of the body adds a property.
  • It hands the object back, so goldie refers to it. The constructor has no return of its own, because new does the handing back.
  • Node’s output names the class: console.log(goldie) shows Plant { ... }, and the word Plant in front names the class that made the object.
  • Each new makes its own object. new Plant("Moss", 7, 3) makes another empty object, separate from Goldie’s, and the constructor fills it with Moss’s values. Changing goldie.daysSinceWatered leaves moss.daysSinceWatered alone.
  • new completes the call forms for this:
The call this is
spikeReminder() (bound) the object it was bound to
goldie.reminder() goldie, the object before the dot
reminder() undefined (strict code)
reminder.call(spike), reminder.apply(spike) spike, the first argument
new Plant(...) the new object

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 You can read this from how a function is called. One call form is still missing, and it uses the new keyword. In this video, you’ll write a class for Mei’s plants and trace what new does, step by step.

00:13 So far, each of Mei’s plants is a plain record. For now, look only at their data. Goldie and Moss both spell out the same three property names: the name, the watering interval, and the days since watered.

00:27 Lily and Spike would need those names typed twice more, and every copy has to match. Mei would rather write the shape of a plant once, and hand over only the values.

00:37 A function that returns the record could do that too. The class pays off in the next lesson, when the methods come back.

00:46 Mei starts a new file, plant.js. She writes the keyword class, then the name Plant. By convention, class names start with a capital letter, so a class stands out from the plants it makes.

00:59 Inside its braces goes the constructor. It’s written like a method, but its name is fixed. The constructor is the code that new runs. Its three parameters are the three values every plant needs.

01:13 Each line in the constructor’s body sets one property on this: the name, the watering interval, and the days since watered. But nothing calls the constructor with an object before the dot.

01:25 So where does this come from?

01:29 Line 9 makes Goldie. It’s new, then Plant, then her three values. The new keyword does three things, in order. First, it makes a fresh, empty object. Second, it runs the constructor, with this set to that object, and Goldie’s values go to the three parameters.

01:50 Each line of the body adds a property, so the object fills up one line at a time. Third, new hands the object back, and goldie refers to it. The constructor has no return of its own.

02:03 It doesn’t need one, because new does the handing back.

02:07 Line 12 logs goldie, and the terminal shows an object with Goldie’s three values. The word Plant in front of it names the class that made it.

02:18 Line 10 makes Moss the same way. New makes another empty object, separate from Goldie’s. The constructor runs again, with this set to the new object, and fills in Moss’s values.

02:31 Then the object goes to moss, and line 13 logs it, with a 7 and a 3.

02:37 Now Mei waters Goldie. Line 15 sets Goldie’s days since watered to 0, and line 16 logs Moss’s count. Your turn: pause the video and predict what line 16 prints.

02:55 It prints 3. Each plant is its own object, made by its own call to new. So changing Goldie’s count leaves Moss’s count alone.

03:07 So when you see new in front of a class name, read it as three steps. Make an object. Run the constructor with this as that object. Hand the object back.

03:20 That also completes the call forms for this. An arrow still takes this from the code it’s written in, whatever the call. A bound function is checked first, and it keeps the object it was bound to.

03:32 With an object before the dot, this is that object, and with no dot, it’s undefined in strict code. Call and apply hand over their first argument.

03:41 And with new, this is the brand new object.

03:46 Mei’s plants hold their own data now, but they can’t do anything yet. Next, in Build Plants From a Class, you’ll give the Plant class its methods and make all four of Mei’s plants.

Become a Member to join the conversation.