Skip to content

290 doc js classes - #333

Open
evolvingkid wants to merge 4 commits into
Planet-NULLCAST:mainfrom
evolvingkid:290-doc-js_classes
Open

290 doc js classes#333
evolvingkid wants to merge 4 commits into
Planet-NULLCAST:mainfrom
evolvingkid:290-doc-js_classes

Conversation

@evolvingkid

@evolvingkid evolvingkid commented Oct 30, 2021

Copy link
Copy Markdown

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:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules
  • I have checked my code and corrected any misspellings

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
@thealpha93
thealpha93 requested a review from misuvi October 30, 2021 15:54
]
---

A javascript class is a blueprint for creating objects. A class encapsulates data and functions that manipulate data.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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


## Class Declaration

We can declare a class by using the class keyword. For example:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constructor in inline code block


```

The constructor will be called when you created the object named **textClassObjects**.

@misuvi misuvi Dec 6, 2021

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'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**.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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**.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace *** with `

case: ["class Calcu {"],
hint: "Please re check the class syntax and code formatting.",
isCorrect: false
},

Copy link
Copy Markdown
Collaborator

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.

},
{
id: 2,
case: ["constructor(num1){"],

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

insert space after ')'

},
{
id: 3,
case: ["this.num1 = num1;"],

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add case without semicolon

},
{
id: 6,
case: ["return this.num1 + num2;"],

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

case without semicolon

},
{
id: 9,
case: ["let calcuObjects = new Calcu(1);"],

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

case without semicolon

},
{
id: 10,
case: ["const calcuNum2 = calcuObjects.toAdd(2);"],

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

case without semicolon

},
{
id: 11,
case: ["console.log(concatData);"],

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

case without semicolon

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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'

- 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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

num1 num2 in backticks

- 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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

calcuObjects

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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

@misuvi misuvi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make the necessary changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants