Skip to content

Commit 9df9daa

Browse files
committed
objects and prototypes notes
1 parent df15c2c commit 9df9daa

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

session 1/chapters/2. objects.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,61 @@ var human = {
2424
console.log(human.fullName()); //Virat Kohli
2525
```
2626

27+
## Different ways of creating a JavaScript object:
28+
29+
1. Using object literal
30+
31+
```javascript
32+
var human = {
33+
firstName: 'Virat',
34+
lastName: 'Kohli',
35+
age: 30,
36+
fullName: function () {
37+
return this.firstName + ' ' + this.lastName;
38+
},
39+
};
40+
```
41+
42+
2. Using new Object()
43+
44+
```javascript
45+
var human = new Object();
46+
console.log(human); // Creates an empty object
47+
```
48+
49+
We can add as many properties as we want usign either the dot notation or the square bracket notation
50+
51+
```javascript
52+
human.firstName = 'Virat';
53+
human.lastName = 'Kohli';
54+
human.age = 30;
55+
human.fullName = function () {
56+
return this.firstName + ' ' + this.lastName;
57+
};
58+
console.log(human);
59+
```
60+
61+
3. Object constructor
62+
63+
```javascript
64+
function Human(firstName, lastName) {
65+
(this.firstName = firstName),
66+
(this.lastName = lastName),
67+
(this.fullName = function () {
68+
return this.firstName + ' ' + this.lastName;
69+
});
70+
}
71+
```
72+
73+
Now you can create as many objects as you want using this constructor function:
74+
75+
```javascript
76+
var viratKohli = new Human('Virat', 'Kohli');
77+
console.log(viratKohli);
78+
var sachinTendulkar = new Human('Sachin', 'Tendulkar');
79+
console.log(sachinTendulkar);
80+
```
81+
2782
**Properties of the object can be accessed using**
2883

2984
1. Dot notation.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# What’s Prototype?
2+
3+
Almost all objects in JavaScript have the prototype property. By using it and more specifically the _prototype chain_ we can mimic inheritance.
4+
5+
The prototype is a reference to another object and it is used whenever JS can’t find the property you’re looking for on the current object.
6+
7+
Simply put, whenever you call a property on an object and it doesn’t exist, JavaScript will go to the prototype object and look for it there. This can bubble up all the way to Object.prototype before returning undefined. This is the essence of the prototype chain and the behavior that sits behind JavaScript’s inheritance.
8+
9+
# Problem with creating objects with the constructor function:
10+
11+
Consider the constructor function below:
12+
```javascript
13+
function Human(firstName, lastName) {
14+
this.firstName = firstName,
15+
this.lastName = lastName,
16+
this.fullName = function() {
17+
return this.firstName + " " + this.lastName;
18+
}
19+
}
20+
21+
var person1 = new Human("Virat", "Kohli");
22+
var person2 = new Human("Sachin", "Tendulkar");
23+
```

0 commit comments

Comments
 (0)