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) { = name;
 this.age = age; = online;
//creates a method we can use for these objects that do special tasks
onlineStatus() {

  if ( === true) {console.log(`${} is online at the moment`)}
  else {console.log(`${} 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

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(`${} is ${ ? `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.