Skip to content

Commit 4bf00d8

Browse files
committed
JavaScript - Classes and Objects
1 parent bfad714 commit 4bf00d8

File tree

1 file changed

+76
-16
lines changed

1 file changed

+76
-16
lines changed

docs/technologies/javascript/working-with-classes-and-objects.mdx

Lines changed: 76 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import SubHeading from "@site/src/components/SubHeading";
99

1010
As you begin your journey into the world of JavaScript and programming, you'll realize that variables, objects, and classes are at the heart of the language. They are the building blocks of any good JavaScript program, and mastering them will set you on the path to becoming a true JavaScript ninja!
1111

12-
![JavaScript Ninja](https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExNGUxN2MwYmYzYTRmNWY0YmNhNzFkOWIzYTY5YWM1ZDg4ZDY0ZDY2NyZlcD12MV9pbnRlcm5hbF9naWZzX2dpZklkJmN0PWc/jzHFPlw89eTqU/giphy.gif)
12+
<br />
13+
14+
## What is a `variable`?
1315

14-
## What is a variable?
1516
A variable is simply an identifier or container that holds a value. It can contain data of different types, such as boolean, string, number, object, and array.
1617

1718
> 💡 A boolean variable holds either True or False, a string variable contains a sequence of characters enclosed in quotation marks, and an array is a collection of values of any data type.
@@ -20,10 +21,16 @@ A variable is simply an identifier or container that holds a value. It can conta
2021

2122
From the image above, the cup represents an empty variable; the milk and coffee are data of different data types. When you put the data into the cup, the cup becomes a variable of data type, coffee or milk.
2223

23-
## How to create variables in JavaScript
24+
<br />
25+
26+
## `Create variables` in JavaScript
27+
2428
To declare (create) variables in JavaScript, you need to use some built-in keywords such as var, let, and const. Let's talk briefly about them.
2529

26-
### The var keyword
30+
<br />
31+
32+
### 👉 The `var` keyword
33+
2734
The var keyword is the oldest way of declaring variables in JavaScript, and it's not advisable to use it in your JavaScript programs because it is not block scope.
2835

2936
> A variable is block-scoped when it only exists within the curly braces where it was declared and cannot be accessed outside the curly braces.
@@ -41,7 +48,11 @@ if(true) {
4148
console.log(myAge) //prints 20
4249
console.log(yourAge) //ReferenceError: yourAge is not defined (Block-scoped)
4350
```
44-
### The let keyword
51+
52+
<br />
53+
54+
### 👉 The `let` keyword
55+
4556
The let keyword is a suitable alternative to var. It is block-scoped and used for declaring variables whose values may change at some point during execution.
4657
```js
4758
let age = 20; //sets a variable to 20
@@ -50,7 +61,10 @@ console.log(age); // prints 5 to the console
5061
```
5162
The code snippet above shows a variable created using the `let` keyword with an initial value of 20, then changed to 5.
5263

53-
### The const keyword
64+
<br />
65+
66+
### 👉 The `const` keyword
67+
5468
The const keyword is used to declare variables that cannot be re-assigned to another value. It holds constant values.
5569
```js
5670
const age = 20;
@@ -59,7 +73,10 @@ console.log(age) //returns a TypeError: Assignment to constant variable. (Inval
5973
```
6074
> 💡 Don’t use the `var` keyword. Use `const` when you need to declare a constant and `let` when the variable can be re-assigned to another value.
6175
62-
### Naming variables in JavaScript
76+
<br />
77+
78+
### 👉 `Naming variables` in JavaScript
79+
6380
When naming variables in JavaScript, there are a few things you need to consider.
6481
- Variables can only start with a letter, underscore, or dollar sign. Numbers and other symbols can come after the first position.
6582
```js
@@ -76,7 +93,11 @@ const const = 24;
7693
/*-- They are all invalid naming conventions --*/
7794
```
7895
- Use variable names that describe the value they hold or contain.
79-
## What are objects in JavaScript?
96+
97+
<br />
98+
99+
## `What are objects` in JavaScript?
100+
80101
An object is an entity that has a collection of properties arranged in key-value pairs. The key is a property name, and every property has a value assigned to it within the object.
81102

82103
![object description](https://user-images.githubusercontent.com/67129211/235659909-9744605b-5620-468e-b0c6-2cd40cf504ea.png)
@@ -92,8 +113,12 @@ const cup = {
92113
The code snippet above creates a JavaScript object for the cup. The name of the JavaScript object is `cup`, and its properties are the key-value pairs.
93114
> 💡 An object can have properties of different data types. The cup object can also have a property of `isBlack: false`.
94115
95-
### Accessing the object properties in JavaScript
116+
<br />
117+
118+
### 👉 `Accessing object properties` in JavaScript
119+
96120
You can access the property of an object using the dot notation or bracket notation.
121+
97122
```js
98123
//👇🏻 Dot notation
99124
console.log(cup.colour) //Output: white
@@ -105,9 +130,11 @@ console.log(cup["colour"]) //Output: white
105130
console.log(cup["material"]) //Output: porcelain
106131
console.log(cup["size"]) ////Output: medium
107132
```
133+
108134
> 💡 In dot notation, you can access each property by using the syntax `object.propertyname`, and in bracket notation, use `object["propertyname"]`.
109135
110136
Use camel case to join property names that are more than one word.
137+
111138
```js
112139
const cup = {
113140
colour: "white",
@@ -119,7 +146,9 @@ const cup = {
119146
console.log(cup.dateCreated) //Output is 02-05-2023
120147
console.log(cup["dateCreated"]) //Output is 02-05-2023
121148
```
149+
122150
In JavaScript objects, if a property name is spaced, you need to put it in quotes, and you can only access its value using the bracket notation.
151+
123152
```js
124153
const cup = {
125154
colour: "white",
@@ -130,8 +159,13 @@ const cup = {
130159

131160
console.log(cup["date created"]) //Output is 02-05-2023
132161
```
133-
### Modifying JavaScript objects
162+
163+
<br />
164+
165+
### 👉 `Modifying` JavaScript objects
166+
134167
A JavaScript object property can be modified by referencing and updating its value. For example, let’s change the material and colour of the cup.
168+
135169
```js
136170
const cup = {
137171
colour: "white",
@@ -154,6 +188,7 @@ console.log(cup)
154188
The code snippet above changes the values of the object properties - colour and material.
155189

156190
Recall that the `const` keyword creates constant variables. Therefore, you can not re-assign an object created with the `const` keyword to another value, but you can change its properties.
191+
157192
```js
158193
const cup = {
159194
colour: "white",
@@ -171,15 +206,20 @@ cup = {
171206

172207
console.log(cup) //returns a TypeError: Assignment to constant variable
173208
```
209+
174210
> 💡 You can only re-assign an object to another value when it is declared using the `let` keyword.
175211
176-
## What are classes in JavaScript?
212+
<br />
213+
214+
## `What are classes` in JavaScript?
215+
177216
A class is a blueprint for creating objects that have the same properties and functions. It provides a template for creating multiple objects with similar features but different values.
178217

179218
![how classes work in JavaScript](https://user-images.githubusercontent.com/67129211/235667320-d586395f-1e12-4939-9961-4af448438234.png)
180219

181220
The image above shows a class of domestic animals containing a dog and a cat. The class provides the blueprint that determines the kind of animals present.
182221
Here is an example of a JavaScript class describing the image above.
222+
183223
```js
184224
class Domestic {
185225
constructor(name, colour, owner) {
@@ -198,11 +238,15 @@ console.log(dog)
198238
console.log(cat)
199239
//Output: Domestic { name: 'Pretty', colour: 'Brown', owner: 'Mrs. Banks' }
200240
```
241+
201242
To create a class in JavaScript, you need to add the `class` keyword followed by the name of the class name in sentence case.
243+
202244
```js
203245
class ClassName {}
204246
```
247+
205248
Next, add a `constructor` method containing the class attributes.
249+
206250
```js
207251
class Domestic {
208252
constructor(name, colour, owner) {
@@ -212,16 +256,23 @@ class Domestic {
212256
}
213257
}
214258
```
259+
215260
From the code snippet above, the class - `Domestic` has name, colour, and owner attributes. The `this` keyword reference any current object initiated from the class and sets its value to the object's parameter.
216261

217262
Finally, you can create new object instances of the class by using the `new` keyword and passing the constructor parameters into the class.
263+
218264
```js
219265
//👇🏻 instances of the class
220266
const dog = new Domestic("Swift", "White", "Mr. Adrian")
221267
const cat = new Domestic("Pretty", "Brown", "Mrs. Banks")
222268
```
223-
### Accessing and modifying instances of a class
269+
270+
<br />
271+
272+
### 👉 Accessing and modifying instances of a class
273+
224274
You can access and modify instances of a class the same way it's done in objects. Moreover, instances of a class are objects of that class.
275+
225276
```js
226277
const dog = new Domestic("Swift", "White", "Mr. Adrian")
227278
const cat = new Domestic("Pretty", "Brown", "Mrs. Banks")
@@ -238,11 +289,20 @@ console.log(cat)
238289
```
239290
The code snippet above changes the name of the cat and dog object after initiation.
240291

292+
<br />
293+
241294
## Key Takeaways
242-
- Variables are like containers that hold data. They allow you to store and manipulate data of different types, such as numbers, strings, or boolean values.
243-
- Objects, on the other hand, are like containers that hold multiple pieces of information - properties. These properties can be anything from strings and numbers to other objects and arrays.
244-
- Classes are templates for objects. They provide a blueprint that you can use to create multiple objects with the same properties and behaviours.
295+
296+
- `Variables` are like containers that hold data. They allow you to store and manipulate data of different types, such as numbers, strings, or boolean values.
297+
- `Objects`, on the other hand, are like containers that hold multiple pieces of information - properties. These properties can be anything from strings and numbers to other objects and arrays.
298+
- `Classes` are templates for objects. They provide a blueprint that you can use to create multiple objects with the same properties and behaviours.
245299

246300
Understanding what variables, objects, and classes are, is essential for a beginner in JavaScript. By mastering these basic concepts, you're well on your way to building more complex programs in JavaScript.
247301

248-
Thank you for reading! 🎉
302+
<br />
303+
304+
## Resources
305+
306+
- 👉 [JavaScript Tutorial](https://developer.mozilla.org/en-US/docs/Web/JavaScript) by Mozilla Foundation
307+
- 👉 Free [Support](https://appseed.us/support/) via Email & Discord
308+
- 👉 [Custom Development Services](https://appseed.us/custom-development/) provided by experts

0 commit comments

Comments
 (0)