You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+152-1Lines changed: 152 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -432,4 +432,155 @@ async function asyncFunc() {
432
432
}
433
433
```
434
434
435
-
The `async` function implicitly returns a promise, so we can use the `then` method to get the value later.
435
+
The `async` function implicitly returns a promise, so we can use the `then` method to get the value later.
436
+
437
+
## Classes
438
+
439
+
Similar to languages like C++, Java, Python etc. JavaScript also supports classes. We use the keyword `new` to create an object of the class. The syntax of the class is given below.
440
+
441
+
```javascript
442
+
functionPerson(name, age) {
443
+
this.name= name;
444
+
this.age= age;
445
+
446
+
this.printDetails=function() {
447
+
console.log(this.name+""+this.age);
448
+
}
449
+
}
450
+
451
+
var person =newPerson("John", 20);
452
+
person.printDetails();
453
+
```
454
+
455
+
There is also one more way to create a object, without using the `new` keyword. This is called the factory method.
456
+
457
+
```javascript
458
+
var person = {
459
+
name:"Anurag",
460
+
age:20,
461
+
printDetails:function () {
462
+
console.log(this.name+""+this.age);
463
+
}
464
+
}
465
+
466
+
console.log(person.name);
467
+
person.printDetails();
468
+
```
469
+
470
+
Both methods can be used to create an object, but the only difference is in the second method we directly create an object named person without creating a class.
471
+
472
+
## Function Context
473
+
474
+
All javascript functions run under a specific context/scope, for example, all the functions which are run in the browser are run under the `window` context. This keyword is used to access the context of the function.
475
+
476
+
But we can change the context of the function using the `bind` method. The `bind` method returns a new function with the context changed.
477
+
478
+
```javascript
479
+
var person = {
480
+
name:"Anurag",
481
+
}
482
+
483
+
functionprintName() {
484
+
console.log(this.name);
485
+
}
486
+
487
+
// We cannot call the printName function directly, because the context of the function is not set.
488
+
489
+
var printNameWithContext =printName.bind(person);
490
+
```
491
+
492
+
There is also another function, which is called `call` function. It is somewhat similar to `bind` but instead of returning the function, it directly calls the function with context.
493
+
494
+
The same example can be written using the `call` function.
495
+
496
+
```javascript
497
+
printName.call(person);
498
+
```
499
+
500
+
In simple terms, you can think of call as the automatic execution of the bind function.
501
+
502
+
```javascript
503
+
printName.call(person)
504
+
printName.bind(person)();
505
+
```
506
+
507
+
Both are same in terms of results. Another example:
508
+
509
+
```javascript
510
+
var printNameContext =printName.bind(person);
511
+
varprintNameCall=function () {
512
+
printName.call(person);
513
+
}
514
+
// both are same
515
+
```
516
+
517
+
## Inheritance
518
+
519
+
An important concept of the object-oriented programming language is inheritance. JavaScript supports inheritance in many different ways. One of the ways is using the `prototype` property of the function.
520
+
521
+
Every object in the javascript has a property called `prototype`
522
+
523
+
### Prototypes
524
+
525
+
Above under the classes section, you can see how to define a class without using any of the inheritance concepts. Now, let me give an example using the prototype property.
0 commit comments