-
Notifications
You must be signed in to change notification settings - Fork 35
290 doc js classes #333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
290 doc js classes #333
Changes from all commits
2d682d8
621c398
e67fed0
702ec2b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,217 @@ | ||
| --- | ||
| title: "Introduction To Javascript" | ||
| subheading: "Class" | ||
| prev: "arrow-functions" | ||
| next: "" | ||
| testCase: [ | ||
| { | ||
| id: 1, | ||
| case: ["class Calcu {"], | ||
| hint: "Please re check the class syntax and code formatting.", | ||
| isCorrect: false | ||
| }, | ||
| { | ||
| id: 2, | ||
| case: ["constructor(num1){"], | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. insert space after ')' |
||
| hint: "Please re check the code and formatting.", | ||
| isCorrect: false | ||
| }, | ||
| { | ||
| id: 3, | ||
| case: ["this.num1 = num1;"], | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add case without semicolon |
||
| hint: "Please re check the csode and formatting.", | ||
| isCorrect: false | ||
| }, | ||
| { | ||
| id: 4, | ||
| case: ["}"], | ||
| hint: "Please re check the code and formatting.", | ||
| isCorrect: false | ||
| }, | ||
| { | ||
| id: 5, | ||
| case: ["toAdd(num2) {"], | ||
| hint: "Please re check the code and formatting.", | ||
| isCorrect: false | ||
| }, | ||
| { | ||
| id: 6, | ||
| case: ["return this.num1 + num2;"], | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. case without semicolon |
||
| hint: "Please re check the code and formatting.", | ||
| isCorrect: false | ||
| }, | ||
| { | ||
| id: 7, | ||
| case: ["}"], | ||
| hint: "Please re check the code and formatting.", | ||
| isCorrect: false | ||
| }, | ||
| { | ||
| id: 8, | ||
| case: ["}"], | ||
| hint: "Please re check the code and formatting.", | ||
| isCorrect: false | ||
| }, | ||
| { | ||
| id: 9, | ||
| case: ["let calcuObjects = new Calcu(1);"], | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. case without semicolon |
||
| hint: "Please re check the code and formatting.", | ||
| isCorrect: false | ||
| }, | ||
| { | ||
| id: 10, | ||
| case: ["const calcuNum2 = calcuObjects.toAdd(2);"], | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. case without semicolon |
||
| hint: "Please re check the code and formatting.", | ||
| isCorrect: false | ||
| }, | ||
| { | ||
| id: 11, | ||
| case: ["console.log(concatData);"], | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. case without semicolon |
||
| hint: "Please re check the code and formatting.", | ||
| isCorrect: false | ||
| }, | ||
| ] | ||
| --- | ||
|
|
||
| A javascript class is a blueprint for creating objects. A class encapsulates data and functions that manipulate data. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. class wrapped in backtick(``) which makes it inline code block. All technical terms need to be wrapped in inline code block |
||
| Javascript supports classes from ES6 own words. Before that, Javascript doesn't have any concept of classes. To mimic the class we often used the constructor/prototype pattern. | ||
|
|
||
| ## Class Declaration | ||
|
|
||
| We can declare a class by using the class keyword. For example: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. inline code block for 'class' |
||
|
|
||
| ```javascript | ||
|
|
||
| // Declaring class | ||
| class TestClass {} | ||
|
|
||
| // create object of the class | ||
| let textClassObject = new TestClass(); | ||
|
|
||
| ``` | ||
|
|
||
| In the above example, the TestClass is the class and `let textClassObject = new TestClass();` is where the we create an object named textClassObject for the TestClass. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. inline code block for 'TestClass', 'textClassObject' |
||
|
|
||
| You can see there is a new keyword used on creating the object of the class. It is used to create a new instance of the TextClass. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TextClass inline code block |
||
|
|
||
|
|
||
| ```javascript | ||
|
|
||
| console.log(textClassObject instanceof TestClass); | ||
| // will return true. | ||
| ``` | ||
|
|
||
| When you are dealing with classes you also need to think that class declarations are not hoisted. This means that you can create an object of the class before initializing it. | ||
|
|
||
| ```javascript | ||
| let textClassObjects = new TestClass(); //ReferenceError | ||
|
|
||
| class TestClass {} | ||
|
|
||
| ``` | ||
|
|
||
| ## Constructor | ||
| The constructor method is a special method inside the class for creating and initializing an object in a class. Javascript automatically calls the constructor method when you initialize an object of the class. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. constructor in inline code block |
||
|
|
||
| ```javascript | ||
| class TestClass { | ||
|
|
||
| constructor(){ | ||
| console.log("constructor is called"); | ||
| } | ||
| } | ||
|
|
||
| let textClassObjects = new TestClass(); | ||
|
|
||
| ``` | ||
|
|
||
| The constructor will be called when you created the object named **textClassObjects**. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. inline code block instead of bold |
||
|
|
||
| You can also pass data through the constructor just like a function. | ||
|
|
||
| ```javascript | ||
|
|
||
| class TestClass { | ||
|
|
||
| constructor(name){ | ||
| console.log(`this is the passes data ${name}`); | ||
| } | ||
| } | ||
|
|
||
| let textClassObjects = new TestClass("nullcast"); | ||
|
|
||
| ``` | ||
| * Please note that you can only create one constructor in a class. | ||
| *** | ||
| ### this keyword | ||
| In JavaScript this keyword refers to the object it belongs to. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'this' in inline code block |
||
|
|
||
| We will have some examples of **this** in **classes**. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace bold with inline codeblock |
||
|
|
||
| ```javascript | ||
|
|
||
| class TestClass { | ||
|
|
||
| name = 'nullcast' | ||
|
|
||
| constructor(name){ | ||
|
|
||
| // this is the same one from class | ||
| console.log(this.name); | ||
|
|
||
| // the name is from the params | ||
| console.log('data from params '+ name); | ||
| } | ||
| } | ||
|
|
||
| let textClassObjects = new TestClass('Ducks'); | ||
|
|
||
| ``` | ||
| In the above example, 'this.name' is used to access the name variable on the class named 'TestClass'. And the 'name' variable is the same variable we need as params in our constructor. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of single quotes, inline code block |
||
|
|
||
| * Please note that they are actually called methods not variables. But for the simple understanding we just called them variables. | ||
| *** | ||
| ## Methods | ||
| Just like we use variables and constructor in our class. We can also create functions too. We called then methods. They looks like a function and act like one for the most part. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. methods in inline code block |
||
|
|
||
| For example: | ||
|
|
||
| ```javascript | ||
|
|
||
| class TestClass { | ||
|
|
||
| constructor(name){ | ||
|
|
||
| this.stringOne = name | ||
| } | ||
|
|
||
| toConcatStrings(stringTwo) { | ||
|
|
||
| return this.stringOne + stringTwo | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
| let textClassObjects = new TestClass('Nullcast'); | ||
|
|
||
| const concatData = textClassObjects.toConcatStrings(' Ducks') | ||
|
|
||
| // result: Nullcast Ducks | ||
| console.log(concatData); | ||
|
|
||
| ``` | ||
|
|
||
| In the above example, we create a class and it has a method named toConcatStrings. In the constructor of that class, you can see we have a param named name and we initialize that value to this.stringOne. But if you take look at the class you can see we didn't declare any variables or method named stringOne. Don't worry it will not cause an error a new method named stringOne will be created for our class. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. toConcatStrings, this.stringOne, stringOne |
||
|
|
||
| You can see we called our method outside of our class by `const concatData = textClassObjects.toConcatStrings(' Ducks')`. By this, we can point out that we can call the method named **toConcatStrings** on the class **textClassObjects**. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. replace *** with ` |
||
|
|
||
| ## Complete the task below | ||
| - create a class named Calcu | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Calcu,, num1 inline code blocks |
||
| - create a constructor with params as num1 | ||
| - make an object name num1 using this keyword and insert the above num1 param to it. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this particular task is ambiguous. Maybe Object property instead of object name, wrap num1 in backticks, and replace 'insert' with 'assign' |
||
| - make a method name toAdd which have a param named num2 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| - it should return the the addition of method num1 and num2 from the param. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. num1 num2 in backticks |
||
| - create an object named calcuObjects of the above class and pass value as 1. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. calcuObjects
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also suggest 'using let', where let is inline code block |
||
| - now call the method toAdd and pass value as 2 and get the return value to a variable named calcuNum2 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. toAdd, 2, calcuNum2 in inline code blocks, suggest 'using const', const in inline code block |
||
| - Then console log calcuNum2 value. | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hint should be like 'Create a class named Calcu', 'add constructor to the class with param num1', 'assign num1 to class property num1'. If you run testcases in the UI, you'll know why.