Skip to content

Commit ec86ed5

Browse files
authored
feat(curriculum): update class lesson with interactive example (freeCodeCamp#63572)
1 parent 1bacf09 commit ec86ed5

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

curriculum/challenges/english/blocks/lecture-understanding-how-to-work-with-classes-in-javascript/6733affc29df1304e2c97e88.md

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ challengeType: 19
55
dashedName: what-are-classes-and-how-do-they-work-in-javascript
66
---
77

8-
# --description--
9-
10-
Let's learn about classes in JavaScript.
8+
# --interactive--
119

1210
In modern JavaScript, classes are like blueprints that you can define in code for creating multiple objects of the same kind.
1311

@@ -81,24 +79,54 @@ You can then access this property using the dot notation, like you can see in th
8179
console.log(dog.name);
8280
```
8381

84-
This will log the value of the `name` property of this particular instance:
82+
Here is the full example:
83+
84+
:::interactive_editor
8585

8686
```js
87-
Gino
87+
class Dog {
88+
constructor(name) {
89+
this.name = name;
90+
}
91+
92+
bark() {
93+
console.log(`${this.name} says woof!`);
94+
}
95+
}
96+
97+
const dog = new Dog("Gino");
98+
console.log(dog.name);
8899
```
89100

101+
:::
102+
90103
You can also call methods. In this example, we are calling the `bark()` method on the `dog` instance.
91104

92105
```js
93106
dog.bark();
94107
```
95108

96-
This will log the following message to the console:
109+
Here is the full example with the method call included:
110+
111+
:::interactive_editor
97112

98113
```js
99-
Gino says woof!
114+
class Dog {
115+
constructor(name) {
116+
this.name = name;
117+
}
118+
bark() {
119+
console.log(`${this.name} says woof!`);
120+
}
121+
}
122+
123+
const dog = new Dog("Gino");
124+
console.log(dog.name);
125+
dog.bark();
100126
```
101127

128+
:::
129+
102130
As mentioned earlier, you can also define classes as a class expression. This is where the class is anonymous and assigned to a variable.
103131

104132
Here is what the earlier example looks like as a class expression:

0 commit comments

Comments
 (0)