290 doc js classes - #333
Conversation
all docs about jus class added
added test cases to the js class doc
the test casses where missing a line
because their is another chap with 14 and 15
| ] | ||
| --- | ||
|
|
||
| A javascript class is a blueprint for creating objects. A class encapsulates data and functions that manipulate data. |
There was a problem hiding this comment.
class wrapped in backtick(``) which makes it inline code block. All technical terms need to be wrapped in inline code block
|
|
||
| ## Class Declaration | ||
|
|
||
| We can declare a class by using the class keyword. For example: |
There was a problem hiding this comment.
inline code block for 'class'
|
|
||
| ``` | ||
|
|
||
| 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. |
There was a problem hiding this comment.
inline code block for 'TestClass', 'textClassObject'
|
|
||
| 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. | ||
|
|
||
| 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. |
There was a problem hiding this comment.
TextClass inline code block
| ``` | ||
|
|
||
| ## 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. |
There was a problem hiding this comment.
constructor in inline code block
|
|
||
| ``` | ||
|
|
||
| The constructor will be called when you created the object named **textClassObjects**. |
There was a problem hiding this comment.
inline code block instead of bold
| * 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. |
There was a problem hiding this comment.
'this' in inline code block
| ### this keyword | ||
| In JavaScript this keyword refers to the object it belongs to. | ||
|
|
||
| We will have some examples of **this** in **classes**. |
There was a problem hiding this comment.
Replace bold with inline codeblock
| 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. |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
methods in inline code block
|
|
||
| ``` | ||
|
|
||
| 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. |
There was a problem hiding this comment.
toConcatStrings, this.stringOne, stringOne
|
|
||
| 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. | ||
|
|
||
| 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**. |
| case: ["class Calcu {"], | ||
| hint: "Please re check the class syntax and code formatting.", | ||
| isCorrect: false | ||
| }, |
There was a problem hiding this comment.
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.
| }, | ||
| { | ||
| id: 2, | ||
| case: ["constructor(num1){"], |
| }, | ||
| { | ||
| id: 3, | ||
| case: ["this.num1 = num1;"], |
| }, | ||
| { | ||
| id: 6, | ||
| case: ["return this.num1 + num2;"], |
| }, | ||
| { | ||
| id: 9, | ||
| case: ["let calcuObjects = new Calcu(1);"], |
| }, | ||
| { | ||
| id: 10, | ||
| case: ["const calcuNum2 = calcuObjects.toAdd(2);"], |
| }, | ||
| { | ||
| id: 11, | ||
| case: ["console.log(concatData);"], |
| 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**. | ||
|
|
||
| ## Complete the task below | ||
| - create a class named Calcu |
There was a problem hiding this comment.
Calcu,, num1 inline code blocks
| ## Complete the task below | ||
| - create a class named Calcu | ||
| - create a constructor with params as num1 | ||
| - make an object name num1 using this keyword and insert the above num1 param to it. |
There was a problem hiding this comment.
this particular task is ambiguous. Maybe Object property instead of object name, wrap num1 in backticks, and replace 'insert' with 'assign'
| - create a class named Calcu | ||
| - create a constructor with params as num1 | ||
| - make an object name num1 using this keyword and insert the above num1 param to it. | ||
| - make a method name toAdd which have a param named num2 |
There was a problem hiding this comment.
name named, toAdd as inline code block, 'which have a' Which accepts a, num2 in inline code block
| - create a constructor with params as num1 | ||
| - make an object name num1 using this keyword and insert the above num1 param to it. | ||
| - make a method name toAdd which have a param named num2 | ||
| - it should return the the addition of method num1 and num2 from the param. |
| - make an object name num1 using this keyword and insert the above num1 param to it. | ||
| - make a method name toAdd which have a param named num2 | ||
| - it should return the the addition of method num1 and num2 from the param. | ||
| - create an object named calcuObjects of the above class and pass value as 1. |
There was a problem hiding this comment.
Also suggest 'using let', where let is inline code block
| - make a method name toAdd which have a param named num2 | ||
| - it should return the the addition of method num1 and num2 from the param. | ||
| - create an object named calcuObjects of the above class and pass value as 1. | ||
| - now call the method toAdd and pass value as 2 and get the return value to a variable named calcuNum2 |
There was a problem hiding this comment.
toAdd, 2, calcuNum2 in inline code blocks, suggest 'using const', const in inline code block
misuvi
left a comment
There was a problem hiding this comment.
Please make the necessary changes
Pull Request Template
Description
Add a new content for JavaScript. The content is about classes in JavaScript.
The changes are with the issues #290
How Has This Been Tested?
Tested with simple qa testing doing the test case.
Checklist: