Skip to content
This repository was archived by the owner on Oct 17, 2025. It is now read-only.

Conversation

@aefhm
Copy link

@aefhm aefhm commented Feb 2, 2018

Updating Class Expression Example

Currently, we use a class declaration.

class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
  area() {
    return this.height * this.width;
  }
}

console.log(new Rectangle(5,8).area());
// expected output: 40

after

var Rectangle = class {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
  area() {
    return this.height * this.width;
  }
}

console.log(new Rectangle(5,8).area());
// expected output: 40

@welcome
Copy link

welcome bot commented Feb 2, 2018

💖 Thanks for opening this pull request! 💖
Here is a list of things that will help get it across the finish line: - If this is a new or updated CSS interactive example, please ensure that you followed the CSS styleguide - If this is a new or updated JavaScript interactive example, please ensure that you followed the JavaScript styleguide - If your changes affects any of the steps in our contribution docs, please also make the relevant changes there.

Copy link
Contributor

@wbamberg wbamberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, thanks for your contribution @aefhm !

@wbamberg wbamberg merged commit 3c2308f into mdn:master Feb 2, 2018
@welcome
Copy link

welcome bot commented Feb 2, 2018

Congrats on merging your first pull request! 🎉🎉🎉

@aefhm aefhm deleted the patch-1 branch February 3, 2018 00:17
@aefhm
Copy link
Author

aefhm commented Feb 3, 2018

Thanks @wbamberg.

@danielhickman
Copy link
Contributor

What is this for? I didn't see this in the style guide and I'm curious. Also, since class is ES6, const or let should be used, right?

@wbamberg
Copy link
Contributor

wbamberg commented Feb 3, 2018

As I understand it, the old example incorrectly used a class statement:

class Thing {
}

rather than a class expression:

var Thing = class {
}

Also, since class is ES6, const or let should be used, right?

I did consider this, but the other examples in the page use var, and it's not like var is outlawed in ES6, it just isn't the only option any more. Looking at the style guide, I see it says:

ES6 examples should use: ... let and const instead of var

...which now seems a little overly-prescriptive. Still, perhaps this should be changed to const.

@danielhickman
Copy link
Contributor

Ahh I see, that makes sense.

I think var is still okay for ES6 examples since anyone using ES6 will be exposed to it often enough that either works but was just checking.

@aefhm
Copy link
Author

aefhm commented Feb 4, 2018

Ah, I see the documentation on declaring with let or const fitting the ES6 patterns. Maybe the examples and page could be updated?

@wbamberg
Copy link
Contributor

wbamberg commented Feb 4, 2018

I would accept a PR to change var to let[1] here. But personally I think it's OK as it is too. var is still a legitimate way to declare variables in some situations, and without more context, you can't say that the example should definitely use let.

[1] Probably not to const though, because one of the main differences between class expressions and class statements is that with class expressions you can redefine the variable, and if you use const you can't do this. At least that's my understanding, I'm not very familiar with these APIs.

@aefhm
Copy link
Author

aefhm commented Feb 5, 2018

Inertia it is. I think we get the same error Identifier 'c' has already been declared.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants