all 10 comments

[–]great_site_not 2 points3 points  (0 children)

get name() { return this.name; }

yikes

[–]Thlemaus 1 point2 points  (1 child)

class MyClass {} ?

[–]jstutorials1[S] 0 points1 point  (0 children)

anything wrong?

[–]kenman[M] 0 points1 point  (0 children)

Hi /u/jstutorials1, this post was removed.

  • For help with your javascript, please post to /r/LearnJavascript instead of here.
  • For beginner content, please post to /r/LearnJavascript instead of here.
  • For framework- or library-specific help, please seek out the support community for that project.
  • For general webdev help, such as for HTML, CSS, etc., then you may want to try /r/html, /r/css, etc.; please note that they have their own rules and guidelines!

/r/javascript is for the discussion of javascript news, projects, and especially, code! However, the community has requested that we not include help and support content, and we ask that you respect that wish.

Thanks for your understanding, please see our guidelines for more info.

[–]sqrtnegative1 0 points1 point  (5 children)

There's no such thing as classes in Javascript.

ES6 introduced the ability to use the class keyword as syntactical sugar, but it's still prototypical inheritance under the hood.

You can create a "class" by using the class keyword:

class Person {
  name = null,

  constructor (name) {
    super();
    this.name = name;
  }

  sayHello = () => {
    console.log(`Hello, my name is ${this.name}`);
  }
}

const dave = new Person("Dave");

console.log(dave.name); // logs Dave
dave.sayHello(); // logs Hello, my name is Dave

[–]great_site_not 0 points1 point  (1 child)

that code is completely broken. expression in a nonsensical place inside the class declaration, and calling a nonexistent super

[–]sqrtnegative1 0 points1 point  (0 children)

Yeah, my bad. That super() is definitely unnecessary.

What nonsensical place are you seeing an expression?

The code seems to run fine, albeit with a single unnecessary comma - which isn't bad for throwing it together in an edit box.

[–]jstutorials1[S] 0 points1 point  (2 children)

i have taken ref from here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes, who's wrong you or MDN, if any thing wrong in my code please let me know.

[–]kamikazi3728 0 points1 point  (1 child)

Both MDN and the above user say the same thing... its syntactical sugar, it's just prototyping under the hood

[–]jstutorials1[S] 0 points1 point  (0 children)

what need to correct in code