Skip to content

Commit 1bf3549

Browse files
committed
used static to create class methods and track total users without making objects
1 parent 459dc9f commit 1bf3549

4 files changed

Lines changed: 178 additions & 0 deletions

File tree

static/README.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# static
2+
static - is a keyword that defines properties or methods that belong to a class itself rather than the object created from that class
3+
class owns anything static and not the the object
4+
5+
## I practiced static keyword using the examples below:
6+
### Example 1:
7+
```javascript
8+
class MathUtilities{
9+
static PI = 3.14159
10+
11+
static getDiamater(radius){
12+
return radius * 2;
13+
}
14+
static getCircumference(radius){
15+
return (this.PI * radius * 2).toFixed(2);
16+
}
17+
static getArea(radius){
18+
return (this.PI * radius * radius).toFixed(2);
19+
}
20+
}
21+
console.log(MathUtilities.PI);
22+
console.log(MathUtilities.getDiamater(15));
23+
console.log(MathUtilities.getCircumference(7));
24+
console.log(MathUtilities.getArea(18));
25+
```
26+
27+
#### output:
28+
```javascript
29+
console.log(MathUtilities.PI); // 3.14159
30+
console.log(MathUtilities.getDiamater(15)); // 30
31+
console.log(MathUtilities.getCircumference(7)); // 43.98
32+
console.log(MathUtilities.getArea(18)); // 1017.88
33+
```
34+
35+
### Explanation of the code:
36+
- I created a class called *MathUtilities* to hold math-related utilities like PI, diameter, circumference, and area.
37+
- I used the *static* keyword so that I could call all properties and methods **directly on the class**, without creating any object instance.
38+
- I declared a static property *PI* and gave it the value 3.14159 so that I could reuse it in all circle calculations.
39+
- I added a static method *getDiameter(radius)* so that the code can return the diameter of a circle by simply multiplying the radius by 2.
40+
- I added a static method *getCircumference(radius)* so that the code can return the circumference using the formula *this.PI * radius * 2*, and I used *this.PI* to refer to the static *PI* within the class.
41+
- I added a static method *getArea(radius)* to return the area of a circle using the formula *this.PI * radius * radius*, formatted to 2 decimal places using .*toFixed(2)*.
42+
- I logged all the methods to the console so I could confirm the calculations worked without needing to instantiate the class.
43+
44+
### Example 2:
45+
```javascript
46+
class User{
47+
static userCount = 0;
48+
49+
constructor(username){
50+
this.username = username;
51+
User.userCount ++;
52+
}
53+
54+
static getUserCount(){
55+
console.log(`There are ${User.userCount} users online`);
56+
}
57+
58+
sayHello(){
59+
console.log(`My username is ${this.username}`);
60+
}
61+
}
62+
63+
const user1 = new User("Linus");
64+
const user2 = new User("Annette");
65+
const user3 = new User("Laura");
66+
67+
user1.sayHello();
68+
user2.sayHello();
69+
user3.sayHello();
70+
71+
console.log(user1.username);
72+
console.log(user2.username);
73+
console.log(user3.username);
74+
75+
User.getUserCount();
76+
console.log(User.userCount);
77+
```
78+
79+
#### output:
80+
```javascript
81+
user1.sayHello(); // My username is Linus
82+
user2.sayHello(); // My username is Annette
83+
user3.sayHello(); // My username is Laura
84+
85+
console.log(user1.username); // Linus
86+
console.log(user2.username); // Annette
87+
console.log(user3.username); // Laura
88+
89+
User.getUserCount(); // There are 3 users online
90+
console.log(User.userCount); // 3
91+
```
92+
93+
### Explanation of the Code:
94+
- I created a class called User to model users, each with a unique *username*.
95+
- I used a *static* property *userCount = 0* so that the class can keep track of how many users have been created.
96+
- I added a *constructor(username)* method so that each time I create a new user, it stores their username in *this.username*, and then increments the global *userCount* by 1.
97+
- I created a static method *getUserCount()* so that the code can print out how many users exist without needing an instance.
98+
- I created an instance method *sayHello()* so each user object can introduce itself using its own *username*.
99+
- I instantiated three users *(user1, user2, and user3)* using the *new* keyword so that each user object has its own data.
100+
- I called *sayHello()* on each user so that they could introduce themselves using their unique username.
101+
- I accessed *User.userCount *and called *User.getUserCount()* so that I could confirm the static counter worked as expected.
102+
103+
## What I learnt from the examples demonstrated above
104+
1. How to declare static properties and methods so they live on the class, not on each object.
105+
2. How this inside a static context refers to the class, while inside an instance method it refers to the current object.
106+
3. How to blend static and instance logic in one class—for example, tracking how many objects have been created.
107+
4. When to choose utility-only classes (all static) versus regular classes with per-object state.

static/index.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>static</title>
7+
<link rel="stylesheet" href="style.css">
8+
</head>
9+
<body>
10+
<script src="index.js"></script>
11+
</body>
12+
</html>

static/index.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// static - is a keyword that defines properties or methods that belong to a class itself rather than the object created from that class
2+
// class owns anything static and not the the object
3+
4+
// example 1
5+
class MathUtilities{
6+
static PI = 3.14159
7+
8+
static getDiamater(radius){
9+
return radius * 2;
10+
}
11+
static getCircumference(radius){
12+
return (this.PI * radius * 2).toFixed(2);
13+
}
14+
static getArea(radius){
15+
return (this.PI * radius * radius).toFixed(2);
16+
}
17+
}
18+
console.log(MathUtilities.PI);
19+
console.log(MathUtilities.getDiamater(15));
20+
console.log(MathUtilities.getCircumference(7));
21+
console.log(MathUtilities.getArea(18));
22+
23+
24+
// example 2
25+
class User{
26+
static userCount = 0;
27+
28+
constructor(username){
29+
this.username = username;
30+
User.userCount ++;
31+
}
32+
33+
static getUserCount(){
34+
console.log(`There are ${User.userCount} users online`);
35+
}
36+
37+
sayHello(){
38+
console.log(`My username is ${this.username}`);
39+
}
40+
}
41+
42+
const user1 = new User("Linus");
43+
const user2 = new User("Annette");
44+
const user3 = new User("Laura");
45+
46+
user1.sayHello();
47+
user2.sayHello();
48+
user3.sayHello();
49+
50+
console.log(user1.username);
51+
console.log(user2.username);
52+
console.log(user3.username);
53+
54+
User.getUserCount();
55+
console.log(User.userCount);
56+

static/style.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body{
2+
font-family: Georgia, 'Times New Roman', Times, serif;
3+
}

0 commit comments

Comments
 (0)