-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprototype_4.js
More file actions
39 lines (23 loc) · 1.06 KB
/
prototype_4.js
File metadata and controls
39 lines (23 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
function Foo() {
}
Foo.prototype.bar = () => {};
const c1 = new Foo();
const c2 = new Foo();
console.log(c1.bar === c2.bar);
/*
This code demonstrates sharing a method via the prototype.
1. Define a constructor function `Foo`.
2. Add a method `bar` to `Foo.prototype`. The method is an arrow function.
3. Create two instances `c1` and `c2` using `new Foo()`.
4. Compare if `c1.bar` and `c2.bar` reference the same function.
Explanation:
- Since `bar` is added to `Foo.prototype`, both instances inherit this exact same function.
- Arrow function does NOT create a new function per instance here, it is shared on the prototype.
- Therefore, `c1.bar === c2.bar` is `true`.
Important note about arrow functions on prototypes:
- Arrow functions do not have their own `this`.
- When used on a prototype, `this` inside the arrow function is lexically bound to
the scope where the prototype was defined (usually the global scope or module scope),
which can cause unexpected behavior if you expect `this` to refer to the instance.
Output: true
*/