Skip to content

Commit 68c4d14

Browse files
committed
proto diff
1 parent 18387f3 commit 68c4d14

File tree

2 files changed

+44
-21
lines changed

2 files changed

+44
-21
lines changed

session 1/chapters/3. Prototypes.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,37 @@ The constructor property points back to the function on which `prototype` object
2525

2626
![sharedProto](../images/sharedProto.jpg)
2727

28-
## Prototype Object or Prototype
28+
# Dunder proto (`__proto__`) vs prototype (`prototype`)
29+
30+
`prototype` is a property of a Function object. It is the prototype or you can say blueprint of objects constructed by that function.
31+
32+
`__proto__` is an internal property of an object, pointing to its prototype. Current standards provide an equivalent Object.getPrototypeOf(obj) method, though the de facto standard `__proto__` is quicker.
33+
34+
**`__proto__` property of an object is a pointer to the object's constructor function's prototype property**
35+
36+
```javascript
37+
function Foo() {}
38+
39+
const dunderProtoEqualsPrototypeOfFunction =
40+
new Foo().__proto__ === Foo.prototype;
41+
42+
const actualObjectDontHavePrototype = new Foo().prototype === undefined;
43+
44+
console.log(
45+
'dunder Proto Equals Prototype Of Function',
46+
dunderProtoEqualsPrototypeOfFunction
47+
);
48+
49+
console.log('actual Object Dont Have Prototype', actualObjectDontHavePrototype);
50+
51+
const fooObject = new Foo();
52+
53+
console.log('fooObject is instance of Foo', fooObject instanceof Foo);
54+
55+
console.log('fooObject is instance of Object', fooObject instanceof Object);
56+
```
57+
58+
# Prototype Object or Prototype
2959

3060
Almost all objects in JavaScript have the prototype property. By using it and more specifically the _prototype chain_ we can mimic inheritance.
3161

@@ -99,7 +129,7 @@ console.log(person1.friends); // Output: "Jadeja, Vijay, Amit"
99129
console.log(person2.friends); // Output: "Jadeja, Vijay, Amit"
100130
```
101131

102-
## So What are the Problems?
132+
# So What are the Problems?
103133

104134
1. Problem with the constructor function: Every object has its own instance of the function
105135
2. Problem with the prototype: Modifying a property using one object reflects the other object also

session 1/server/index.js

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
1-
//Create an empty constructor function
2-
function Person() {}
3-
//Add property name, age to the prototype property of the Person constructor function
4-
Person.prototype.name = 'Ashwin';
5-
Person.prototype.age = 26;
6-
Person.prototype.friends = ['Jadeja', 'Vijay']; //Arrays are of reference type in JavaScript
7-
Person.prototype.sayName = function () {
8-
console.log(this.name);
9-
};
1+
function Foo() {}
102

11-
//Create objects using the Person constructor function
12-
var person1 = new Person();
13-
var person2 = new Person();
3+
const dunderProtoEqualsPrototypeOfFunction =
4+
new Foo().__proto__ === Foo.prototype;
145

15-
//Add a new element to the friends array
16-
person1.friends.push('Amit');
6+
const actualObjectDontHavePrototype = new Foo().prototype === undefined;
177

18-
console.log(person1.friends); // Output: "Jadeja, Vijay, Amit"
19-
console.log(person2.friends); // Output: "Jadeja, Vijay, Amit"
8+
console.log(
9+
'dunder Proto Equals Prototype Of Function',
10+
dunderProtoEqualsPrototypeOfFunction
11+
);
2012

21-
person1.friends = ['akash'];
13+
console.log('actual Object Dont Have Prototype', actualObjectDontHavePrototype);
2214

23-
console.log(person1.friends); // Output: "Jadeja, Vijay, Amit"
24-
console.log(person2.friends); // Output: "Jadeja, Vijay, Amit"
15+
const fooObject = new Foo();
16+
console.log('fooObject is instance of Foo', fooObject instanceof Foo);
17+
console.log('fooObject is instance of Object', fooObject instanceof Object);

0 commit comments

Comments
 (0)