Skip to content

Commit 32d2098

Browse files
committed
checking prototype of an object
1 parent f443d46 commit 32d2098

File tree

5 files changed

+90
-11
lines changed

5 files changed

+90
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
The below is also an example of what 'this' refers to when I define a constructor function with new keyword. HERE, `this` will refer to an object that inherits from `instance.prototype`
2+
3+
Here,
4+
5+
```js
6+
function Person(first, last) {
7+
this.first = first
8+
this.last = last
9+
10+
this.displayName = function() {
11+
console.log(`Full name : ${this.first} ${this.last}`)
12+
}
13+
}
14+
15+
// Note in above, I can not use arrow function as I can not create a constructor function with arrow syntax
16+
17+
let person1 = new Person("John", "Reed")
18+
let person2 = new Person("Rohan", "Paul")
19+
20+
person1.displayName() // Full name : John Reed
21+
person2.displayName() // Full name : Rohan Paul
22+
```
23+
24+
So to know the prototype of the instance
25+
26+
```js
27+
console.log(person1.prototype) // undefined
28+
29+
Object.getPrototypeOf(person1) // Person {}
30+
31+
person1.__proto__ // Person {}
32+
```
33+
34+
Another example
35+
36+
```js
37+
function Student() {
38+
this.name = "John"
39+
this.gender = "M"
40+
}
41+
42+
var studObj = new Student()
43+
44+
console.log(Student.prototype) // Student {}
45+
console.log(studObj.prototype) // undefined
46+
console.log(studObj.__proto__) // Student {}
47+
console.log(Object.getPrototypeOf(studObj)) // Student {}
48+
49+
console.log(typeof Student.prototype) // object
50+
console.log(typeof studObj.__proto__) // object
51+
52+
console.log(Student.prototype === studObj.__proto__) // true
53+
```
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
// https://youtu.be/OOC-ypVnHAY?t=10m35s - That Indian looking guy, who takes mock interview in YT.
22
// Problem - write a function print() which will be implemented like this myArr.print() and it will print all the elements of the array
33

4-
// SOL-1
4+
// SOL-1 - Not a good solution as I dont need the extra step of pushing to the resultArr
55
Array.prototype.print = function() {
6-
let resultArr = []
7-
this.forEach(i => resultArr.push(i))
8-
return console.log(resultArr.join());
6+
let resultArr = []
7+
this.forEach(i => resultArr.push(i))
8+
return console.log(resultArr.join())
99
}
1010

1111
myArr = [1, 2, 3]
12-
myArr.print() // => 1,2,3
12+
myArr.print() // => 1,2,3
1313

1414
// SOL-2
1515
Array.prototype.print_1 = function() {
16-
return this.forEach(i => console.log(i))
16+
return this.forEach(i => console.log(i))
1717
}
1818

19-
2019
myArr.print_1()
2120

2221
/* OUTPUT
2322
1
2423
2
25-
3 */
24+
3 */

Javascript/js-basics/this-keyword.js renamed to Javascript/js-basics/this-keyword-1.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,15 @@ function f2() {
3232

3333
f2() === undefined // true
3434

35-
/* Now the case when “this” Refers to a New Instance
35+
/* Now the case when “this” Refers to a New Instance - HERE, `this` will refer to an object that inherits from `instance.prototype` . Meaning with exact JS syntax
36+
37+
Object.getPrototypeOf(instance)
38+
39+
OR
40+
41+
instance.__proto__
42+
43+
( I can print the above 2 expression to get the prototype of the instance)
3644
3745
When a function is invoked with the new keyword, then the function is known as a constructor function and returns a new instance. In such cases, the value of this refers to a newly created instance.
3846
For example: */
@@ -54,7 +62,17 @@ let person2 = new Person("Rohan", "Paul")
5462
person1.displayName() // Full name : John Reed
5563
person2.displayName() // Full name : Rohan Paul
5664

57-
/* In the case of person.displayName, this refers to a new instance person, and in case of person2.displayName(), this refers to person2 (which is a different instance than Person). */
65+
/* In the case of person1.displayName, this refers to a new instance of person1, and in case of person2.displayName(), this refers to person2 (which is a different instance than Person).
66+
67+
I can check what the the prototype object of person1 with below
68+
69+
console.log(person1.prototype) // undefined
70+
71+
Object.getPrototypeOf(person1) // Person {}
72+
73+
person1.__proto__ // Person {}
74+
75+
*/
5876

5977
// NOW AS A SIDE POINT - To pass the value of this from one context to another, use call(), or apply():
6078

Javascript/js-basics/this-keyword-simples-examples.md

Whitespace-only changes.

Javascript/js-data-types/data-types-Number-Famous-Question.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,18 @@ The crux of the problem is that numbers are represented in this format as a whol
2424

2525
For 0.1 in the standard binary64 format, the representation can be written exactly as
2626

27+
```
2728
0.1000000000000000055511151231257827021181583404541015625 in decimal, or
2829
0x1.999999999999ap-4 in C99 hexfloat notation.
30+
31+
```
32+
2933
In contrast, the rational number 0.1, which is 1/10, can be written exactly as
3034

35+
```
3136
0.1 in decimal, or
32-
0x1.99999999999999...p-4 in an analogue of C99 hexfloat notation, where the ... represents an unending sequence of 9's.
37+
0x1.99999999999999...p-4
38+
39+
```
40+
41+
in an analogue of C99 hexfloat notation, where the ... represents an unending sequence of 9's.

0 commit comments

Comments
 (0)