JavaScript class syntax help?

Creating objects using JavaScript class syntax. keep getting a syntax error for some reason. Any help would be appreciated.

//using javascript class syntax to create and manage numerous objects at the same time.
class player {
//creates properties of the object we can plug in
 constructor(name,age,online,onlineStatus) {
 this.name = name;
 this.age = age;
 this.online = online;
}
//creates a method we can use for these objects that do special tasks
onlineStatus() {

  if (this.online === true) {console.log(`${this.name} is online at the moment`)}
  else {console.log(`${this.name} is offline at the moment``)}
}
}

//creating a new object from scratch using javascript class syntax. You can create objects cery fast this way.
var player1 = new player('Chris', '23', true);
//calling the method on our object
player1.onlineStatus();

Something looks amiss, herewith. As does this…

1 Like

Thank you for helping me, I mistaken the method for a parameter. works like a charm.

1 Like

You’re welcome!

Extra Study

The role of the parameters is one thing to understand; easiest done when we envision them as instance variable values. First rule of thumb, give constructors only those parameters as needed. Moot now…

Since we’re discussing classes it may be safe to assume that we understand ternary expression logic. Further, we must by now have a grasp of truthiness. These two concepts combined permit us to revise/refactor the onlineStatus method.

console.log(`${this.name} is ${this.online ? `online` : `offline`} at the moment`);

In the above we isolate concerns. Logging is the final outcome. On the term that is toggled we use a ternary expression with a truthy condition to determine it. The result is a string that we can log.

1 Like

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.